• Resolved mzimmers

    (@mzimmers)


    I’ve been trying to get wp_list_pages() to work the way I want it to, and it occurred to me that part of my problem may be a faulty assumption: that a Sub-Page is the same as a Page. Is this true?

    If this is true, then why does it say here:

    wp_list_pages

    That the code in the second box won’t work when on a child Page?

    Thanks.

Viewing 15 replies - 1 through 15 (of 26 total)
  • Only difference between a child page and a parent page is that the child page has the ‘post_parent’ field in the wp_posts table set to the parent page.

    That code described in wp_list_pages() only shows the ‘child pages’ of a given Page.

    The only difference is that a child Page has a parent assigned to it. Otherwise, they’re the same.

    RE: Codex code

    Try scoping $post to global at the start of that code statement:

    <?php
    global $post;
    $children = wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0');
    if ($children) { ?>
    <ul>
    <?php echo $children; ?>
    </ul>
    <?php } ?>

    And note the description of this code should really state:

    “The following example will generate a list only if there are indeed subpages for the current Page

    Thread Starter mzimmers

    (@mzimmers)

    Kafka: I noticed in the examples that post isn’t explicitly assigned, as I’ve seen it elsewhere. Does casting it to global automatically “take care” of it containing all of the correct fields, or should I have something like:

    $post = $wp_query->post;

    before the other statements?

    Also: my remark about the documentation was really referring to this line:

    “The above examples will only show the children from the parent page, but not when actually on a child page.”

    I can’t understand why this would be true.

    $post and $wp_query should both be globally extant in any WordPress template (and in many cases, it should already be scoped for use). For our purposes here, { $post } and { $post = $wp_query->post } are interchangeable.

    RE: Also

    $post->ID (or $wp_query->post->ID) holds the ID for the current post or Page. So:

    child_of=$post->ID

    Tries to find children of the current Page you are on. To collect the child Pages for a parent, even when on a child Page of that parent:

    https://www.ads-software.com/support/topic/89781#post-455439

    Thread Starter mzimmers

    (@mzimmers)

    Thanks for the clarification. Now: I’ve been assuming that when you visit a sub-Page, that $post will now refer to that sub-Page, NOT its parent Page. True?

    when you visit a sub-Page, that $post will now refer to that sub-Page, NOT its parent Page.

    Correct. On any single post or Page (child or not), the $post object holds the post data for it. Elsewhere it will relate to the ‘current’ post at each iteration of The Loop.

    Thread Starter mzimmers

    (@mzimmers)

    OK. Then WHY (and I know I’m going to feel stupid when I hear the answer) does this not work?

    global $post;
    		if ($post->post_parent!=0)
            {
                $parent = wp_list_pages("title_li=&echo=0&include=".$post->post_parent);
            }
    
    		$thispage = wp_list_pages("title_li=&echo=0&include=".$post->ID); 
    
            $children = wp_list_pages("title_li=&echo=0&sort_column=menu_order&child_of=".$post->ID); 
    
    		if ($parent)
    		{
    			echo "PARENT " . $post->post_parent . $parent . $newline;
    		}
    
    		echo "THISPAGE " . $post->ID . $thispage . $newline;
    
    		if ($children)
            {
    			echo "CHILDREN " . $children . $newline;
            }
    		?>

    On a top-level Page, it’s fine. On a sub-Page, the wp_list_pages doesn’t return a title. And on a sub-sub-Page, nothing returns except my telltale.

    https://www.scopedin.com/wordpress

    Thanks…this one’s been driving me nuts…

    <?php
    global $post;
    if ($post->post_parent!=0) {
    	$parent = wp_list_pages("title_li=&echo=0&include=".$post->post_parent);
    	$children = wp_list_pages("title_li=&echo=0&sort_column=menu_order&child_of=".$post->post_parent);
    } else {
    	$children = wp_list_pages("title_li=&echo=0&sort_column=menu_order&child_of=".$post->ID);
    }
    
    $thispage = wp_list_pages("title_li=&echo=0&include=".$post->ID); 
    
    if ($parent) {
    	echo "PARENT " . $post->post_parent . $parent . $newline;
    }
    
    echo "THISPAGE " . $post->ID . $thispage . $newline;
    
    if ($children) {
    	echo "CHILDREN " . $children . $newline;
    }
    ?>

    Again, the issue is $post->ID, in this case for your:

    $children = wp_list_pages("title_li=&echo=0&sort_column=menu_order&child_of=".$post->ID);

    In mine $children is now set based on the ($post->post_parent!=0)conditional.

    Child-child-Pages (grandchildren?) would need a bigger rewrite.

    Thread Starter mzimmers

    (@mzimmers)

    OK…I must have a fundamental misunderstanding of the $post structure. If $post is pointing to the “current” Page, why then, when the current “page” is a child, is it necessary to include the “child_of” parameter? That would seem to pick up the sub-sub-Pages if one is currently on a sub-Page.

    This is why I asked if a Page and a Subpage were the same. From your code, it seems as though it’s always pointing to the top-level Page. What am I missing?

    Thanks for your patience, Kafka…I’m sure I’ll get this someday.

    EDIT: in case it wasn’t clear, I intended my code snippet to be able to work with three generations of Pages: the current Page (thispage), a parent (if it existed), and any child Pages (even sub-sub-Pages).

    why then, when the current “page” is a child, is it necessary to include the “child_of” parameter?

    I assumed you were attempting to display links to the child Pages of the(ir) immediate parent Page. However, it’s your code. You should know what you want it to do; I can only interpret your intentions.

    That would seem to pick up the sub-sub-Pages if one is currently on a sub-Page.

    *If* you are using the ‘child_of’ parameter with the post ID of the current Page (that is a child of another). If you instead provide the post_parent value of $post for ‘child_of’ it will then list the children of that post (i.e. Page) parent.

    And as noted, working with three levels of Pages would require a rewrite of your code. It is only really of benefit (as is) for two levels, that of parent and child(ren).

    Thread Starter mzimmers

    (@mzimmers)

    OK…what I was trying to do was to display:

    • a parent for the current page (if there was a parent)
    • the current page (title)
    • any children of the current page

    I’m doing this because I’m trying to give the reader a bit of context for where he is in the page heirarchy, plus a nice, quick way to go up one level.

    I really don’t see why my code won’t work for this. It’s not that I don’t believe you (I really do, I promise), but, for my education, can you explain to me *why* it won’t work?

    To be clearer then, you don’t care (at least in the code) about all levels of parent->child->child relationships, you only want it to focus on the relationship for the current Page one is on?

    So, on the top level parent Page, you display parent and children, then on one of the child Pages (to the top level parent), you focus on the current Page as a possible parent to other child Pages, etc.

    Does this read how you expect the code to work?

    Thread Starter mzimmers

    (@mzimmers)

    To be clearer then, you don’t care (at least in the code) about all levels of parent->child->child relationships, you only want it to focus on the relationship for the current Page one is on?

    Precisely. Originally, I was hoping to keep all top-level Pages visible in the sidebar (where this code is executing), but that seemed like more work, so I came to the parent-current-children model as what I thought was an easy fallback.

    So, on the top level parent Page, you display parent and children, then on one of the child Pages (to the top level parent), you focus on the current Page as a possible parent to other child Pages, etc.

    Right, except that I would like to show the parent for that current page as well. That’s the reason I had the $parent code.

    Does this read how you expect the code to work?

    Yes, but it’s not doing it. Take for example, this child-Page:

    Stories

    This first-level child displays the parent’s title, but not its own.
    Or this example:

    Alaskan

    This sub-sub-Page doesn’t show anything!

    I put my original code back in the sidebar file; your changes are temporarily commented out.

    <?php
    global $post;
    
    if( $post->post_parent ) {
    	$parent = wp_list_pages("title_li=&echo=0&hierarchical=0&include=" . $post->post_parent);
    	echo "PARENT " . $post->post_parent . $parent . $newline;
    }
    
    $thispage = wp_list_pages("title_li=&echo=0&hierarchical=0&include=" . $post->ID);
    $children = wp_list_pages("title_li=&echo=0&depth=1&sort_column=menu_order&child_of=" . $post->ID); 
    
    echo "THISPAGE " . $post->ID . $thispage . $newline;
    
    if ($children) {
    	echo "CHILDREN " . $children . $newline;
    }
    ?>

    There’s a small mod to the logic dealing with $parent. However, the real change was in getting around what looks like a bug in get_pages()! I found once we set ‘hierarchical’ to 0 (zero), things work as expected. Several parameters are auto-set to null values when there is an ‘include’ in the arguments. But hierarchical is not one of them.

    In any case, the above should now work. Meanwhile, I’m off to file a bug report…

    Thread Starter mzimmers

    (@mzimmers)

    Aha! I knew that out of my incessant babbling, at least one minor thing of value would emerge.

    1. I searched for this get_pages function you mentioned, and I got some allusions to it, but no real documentation. Is this a behind-the-scenes function?

    2. This is still only a two-deep solution, correct?

    3. I think something’s wrong with the symbol $parent_id in the second echo statement; it doesn’t produce anything.

    This is major progress, and very exciting. Thanks for the help!

Viewing 15 replies - 1 through 15 (of 26 total)
  • The topic ‘is a Sub-Page just a Page?’ is closed to new replies.