• Resolved Wabsnasm

    (@wabsnasm)


    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?).

Viewing 11 replies - 1 through 11 (of 11 total)
  • When I set a page to be a posts page

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

    Thread Starter Wabsnasm

    (@wabsnasm)

    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.

    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;?>

    Thread Starter Wabsnasm

    (@wabsnasm)

    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.

    Thread Starter Wabsnasm

    (@wabsnasm)

    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!

    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.

    Thread Starter Wabsnasm

    (@wabsnasm)

    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?

    See my code example above.

    Thread Starter Wabsnasm

    (@wabsnasm)

    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?

    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.

    Thread Starter Wabsnasm

    (@wabsnasm)

    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.

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Posts page does not show its children’ is closed to new replies.