Forums

[resolved] Posts page does not show its children (12 posts)

  1. Wabsnasm
    Member
    Posted 4 months ago #

    In my page.php template, I include a sidebar that displays all the page's children in a sub-nav. When I set a page to be a posts page (in Settings / Reading), it doesn't show its child pages any more. Is this right? Is it still using page.php, or is it using something else (archive.php, index.php, maybe?).

  2. esmi
    Theme Diva & Forum Moderator
    Posted 4 months ago #

    When I set a page to be a posts page

    Your main posts page uses the index.php template file.

  3. Wabsnasm
    Member
    Posted 4 months ago #

    Thanks.

    Do I have access to $post->ID outside of the loop on that page, given it's actually been created as a page in WordPress? My sidebar is included on index.php too, but still doesn't seem to be showing the child pages.

  4. esmi
    Theme Diva & Forum Moderator
    Posted 4 months ago #

    Try something like:

    <?php if( get_option( 'show_on_front' ) == 'page') :
    $blog_page_id = get_option( 'page_for_posts' );
    $args = array(
        'child_of' => get_option( 'page_for_posts' )
    );
    $blog_children = get_pages( $args );
    echo '<pre>';
    print_r($blog_children);
    echo '</pre>';
    endif;?>
  5. Wabsnasm
    Member
    Posted 4 months ago #

    Cheers esmi, I'll give it a go. Hopefully I only need to find the current page ID and my own existing code can pick up the rest, so first I'll try

    $blog_page_id = get_option( 'page_for_posts' );

    The code will also have to live with the existing page subnav code, something like:

    if( this is a page ) {
        get the page id
    } else if( this is a posts page ) {
        get 'page_for_posts'
    } else {
        fallback
    }

    Anyway, I will try something with your suggestion and post back.

  6. Wabsnasm
    Member
    Posted 4 months ago #

    Brilliant. It appears that on the posts page, $post->ID is 1, even though the 'real' ID of the page is not. So I use that as a part of my test:

    if( 1 === $id && get_option( 'show_on_front' ) == 'page' ) {
        $id = get_option( 'page_for_posts' );
    }

    So it slots nicely into the rest of my code that uses $id (I've got it all wrapped in a 'subnav' class).

    Thanks for your help!

  7. esmi
    Theme Diva & Forum Moderator
    Posted 4 months ago #

    So I use that as a part of my test

    You might want to re-think that approach. It may produce unpredictable results - especially in future versions of WordPress.

  8. Wabsnasm
    Member
    Posted 4 months ago #

    You might want to re-think that approach. It may produce unpredictable results - especially in future versions of WordPress.

    OK, then, I need to develop some other kind of test. So, at the moment, I've got $id, which is either

    1. The ID of a normal page, or
    2. 1 when I'm on the posts page

    How can I test without involving the 1 === $id line?

  9. esmi
    Theme Diva & Forum Moderator
    Posted 4 months ago #

    See my code example above.

  10. Wabsnasm
    Member
    Posted 4 months ago #

    See my code example above.

    That only gives me the ID of the posts page. It doesn't let me test whether we're currently on that page.

    At the moment, when we're on the posts page, $post->ID is 1. I can't get the children of 1, so when $id === 1, I get the $id of the posts page (using the code above taken from your example), and get the children of that instead, giving me what I want. You're saying I shouldn't rely on that test. How else can I determine that we're on the posts page?

  11. esmi
    Theme Diva & Forum Moderator
    Posted 4 months ago #

    Your main post page will use the index.php template file, so a check to see if it is the main posts page is unnecessary.

  12. Wabsnasm
    Member
    Posted 4 months ago #

    Your main post page will use the index.php template file, so a check to see if it is the main posts page is unnecessary.

    Well, my code is in sidebar.php, which is why I asked. Anyway, your last post has made the penny drop in that I can simply use is_home() and is_archive() to achieve what I want (to test whether I'm on one of those pages). If I'm on one of those, then I'll just use the posts page id retrieved as in the code above.

    Thanks for the help!

    CORRECTION: changed is_front_page() to is_home() above.

Reply

You must log in to post.

About this Topic