Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when using frontpage option in WordPress #280

Open
pdenissen opened this issue Nov 15, 2021 · 5 comments
Open

Error when using frontpage option in WordPress #280

pdenissen opened this issue Nov 15, 2021 · 5 comments

Comments

@pdenissen
Copy link

Hi there,

I'm pretty new to Nextjs. This is a great starter point for getting into Nextjs combined with WP.
I am trying to set a page as the homepage. For this in WP there is this setting to set a static page as the frontpage.
When I do this, the homepage isn't changing.

If I click on one of my menu-items i'm getting the error:
Error: A required parameter (slugParent) was not provided as a string in getStaticPaths for /[slugParent]/[[...slugChild]]

I'm not sure where to start.
I have tried to change the src/pages/index.js. But I wasn't sure if that's the right approach.

I hope someone can point me in the right direction.

Thank you!

@colbyfayock
Copy link
Owner

hey @pdenissen the starter isn't configured to dynamically create a frontpage based on the wordpress setting, you would have to customize that yourself inside of Next.js by updating the index.js file like it seems like you were trying tod o

it looks like you have a menu item though that doesn't exist as a page?

@pdenissen
Copy link
Author

Hej @colbyfayock,
Thanks for your reply. Good to know that it's best practise to set it up myself.

According the error; when I don't set a frontpage in the wp-admin, than my links to my static pages are working fine. However, when I do set it to 'frontpage' than i'm getting the error when I click on one of the links in my menu.

If you need any specific information on this, let me know.

Thanks!

@colbyfayock colbyfayock changed the title How to set page as frontpage? Error when using frontpage option in WordPress Nov 29, 2021
@jonnymaceachern
Copy link

jonnymaceachern commented May 18, 2022

@pdenissen Did you figure this out?

My frontpage is a standard WordPress Page and no different than any other page, except that it's set as the Homepage setting in WP Dashboard > Settings > Reading:

CleanShot 2022-05-18 at 14 03 59

However, setting the Homepage results in the following:

  1. An error when navigating to any Page that isn't the homepage

Error: A required parameter (slugParent) was not provided as a string in getStaticPaths for /[slugParent]/[[...slugChild]]

  1. The homepage (/) doesn't use the same template as /[slugParent]/[[...slugChild]].js. I would like this to since it's a standard page and uses the same Gutenberg blocks.

@colbyfayock
Copy link
Owner

@jonnymaceachern for the page template, you can take an approach similar to what i did with Archives, where you can create a Page template and use it as a component

https://github.com/colbyfayock/next-wordpress-starter/blob/main/src/templates/archive.js
https://github.com/colbyfayock/next-wordpress-starter/blob/main/src/pages/posts.js

@fadichmn
Copy link

When using graphql there is a field called isFrontPage inside the pages query. Simply add it after menuOrder on line 19 inside src/data/pages.js

Then this is my modified index.js where I added a simple if statement where the page is a frontpage.

import useSite from 'hooks/use-site';
import { getAllPages } from 'lib/pages';
import { WebsiteJsonLd } from 'lib/json-ld';

import Layout from 'components/Layout';
import Header from 'components/Header';
import Section from 'components/Section';
import Container from 'components/Container';

export default function Home({ pages }) {
  const { metadata = {} } = useSite();
  const { title } = metadata;

  return (
    <Layout>
      <WebsiteJsonLd siteTitle={title} />
      <Header></Header>

      <Section>
        <Container>
          {pages.map((page) => {
            if (page.isFrontPage) {
              return (
                <div key={page.id}>
                  <h2>{page.title}</h2>
                  <div dangerouslySetInnerHTML={{ __html: page.content }} />
                </div>
              );
            }
          })}
        </Container>
      </Section>
    </Layout>
  );
}

export async function getStaticProps() {
  const { pages } = await getAllPages({
    queryIncludes: 'all',
  });
  return {
    props: {
      pages,
    },
  };
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants