WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] is a Sub-Page just a Page? (27 posts)

  1. mzimmers
    Member
    Posted 6 years ago #

    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.

  2. MichaelH
    Member
    Posted 6 years ago #

    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.

  3. Kafkaesqui

    Posted 6 years ago #

    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"

  4. mzimmers
    Member
    Posted 6 years ago #

    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.

  5. Kafkaesqui

    Posted 6 years ago #

    $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:

    http://wordpress.org/support/topic/89781#post-455439

  6. mzimmers
    Member
    Posted 6 years ago #

    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?

  7. Kafkaesqui

    Posted 6 years ago #

    "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.

  8. mzimmers
    Member
    Posted 6 years ago #

    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.

    http://www.scopedin.com/wordpress

    Thanks...this one's been driving me nuts...

  9. Kafkaesqui

    Posted 6 years ago #

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

  10. mzimmers
    Member
    Posted 6 years ago #

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

  11. Kafkaesqui

    Posted 6 years ago #

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

  12. mzimmers
    Member
    Posted 6 years ago #

    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?

  13. Kafkaesqui

    Posted 6 years ago #

    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?

  14. mzimmers
    Member
    Posted 6 years ago #

    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.

  15. Kafkaesqui

    Posted 6 years ago #

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

  16. mzimmers
    Member
    Posted 6 years ago #

    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!

  17. Kafkaesqui

    Posted 6 years ago #

    1. Yep. It's a general, internal function several others (such as wp_list_pages()) act as wrappers to.

    2. Yup. It will display the parent (if any), current and child pages (if any), relative to the current Page. I'd have to locate my thinking cap for a deep diving version. Not sure where it is right now...

    Seriously, if you want to find a parent 2 (or more) levels up, that data doesn't exist with the current $post object. So one has to start querying the database to get at it.

    2. Yelp. Leftover stuff from *my* testing while tracking down the bug. I've edited it out.

  18. mzimmers
    Member
    Posted 6 years ago #

    OK, well, I can get working on a three-level solution, but before I do, there's still something not quite right here. When I navigate to a sub-Page, its title doesn't get pulled in by the wp_get_pages. Similarly, when I navigate to a sub-sub-Page, neither its title, nor its parent's title, gets pulled in.

    The behavior is consistent now...it's just not quite correct. Any ideas?

  19. Kafkaesqui

    Posted 6 years ago #

    Which code are you using?

    Using the last stuff I pasted up, on my test server I visit a top level parent page and see:

    # THISPAGE 1207
    -Visuals
    # CHILDREN
    -Wallpapers
    -Rachael Leigh Cook Image Project (!)

    Clicking through to Wallpapers (where I placed a child Page just for you), I get:

    # PARENT 1207
    -Visuals
    # THISPAGE 1208
    -Wallpapers
    # CHILDREN
    -Custom Fields (Don't ask!)

    Then once again, hitting Custom Fields:

    # PARENT 1208
    -Wallpapers
    # THISPAGE 1264
    -Custom Fields

    That looks right to me (note: using 2.3.1).

  20. mzimmers
    Member
    Posted 6 years ago #

    Well, I must be doing something wrong, then -- the code in my l_sidebar.php file is identical to what you posted (after your edit). But, if I were using your examples, clicking on Wallpapers would not have the "-Wallpapers" line that you get. And, clicking on Custom Fields would not have either the "-Wallpapers" or the "-Custom Fields" lines.

    Could it have something to do with my formatting? Seems unlikely, I know, but...

    it's at http://www.scopedin.com/wordpress if you want to see for yourself. Go to "Reviews" then to "Firearm Reviews" then to "Ruger Super Redhawk Alaskan." You'll see both mistakes in that sequence.

  21. Kafkaesqui

    Posted 6 years ago #

    Are you *absolutely* sure your template is being updated on the server? Because looking at your site, I see exactly what happens when 'hierarchical=0' is not passed as an argument. And that is the only real difference between my code and yours.

  22. mzimmers
    Member
    Posted 6 years ago #

    Are you asking whether I'm sure that I'm uploading my edited l_sidebar.php file?

    Because if you're not asking that, I don't understand the question.

  23. Kafkaesqui

    Posted 6 years ago #

    I'm asking when uploading that template, are you sure it's being overwritten on the server. FTP can be twitchy about that at times.

  24. mzimmers
    Member
    Posted 6 years ago #

    Sigh. Deus Ex Machina strikes again.

    I downloaded the server copy, compared it with my local version, and they were identical.

    So...I cut out all of the code I'd put in, pasted in yours, and...voila.

    I had something subtle out of kilter in my version. No idea what it was, and, as much as I'd enjoy the academic exercise of trying to find out what it was...I think it's time to move on.

    I'll mark this resolved. Since I only want one level above and below the current page, this doesn't need any enhancement, so I'll mark this resolved and go on to other matters like indentation and background colors.

    Thanks for all of your help on this, Kafka.

  25. wlindley
    Member
    Posted 6 years ago #

    I have written a plugin that depends on hierarchical=1 along with include= ... specifically, I create a page-list that shows an indented "breadcrumb-style" list of pages from top-level down to your current location, along with all the current page's "brothers" and "uncles" etc. up to the main level.

    The patch #5373 breaks this functionality, because my "&hierarchical=1" gets overridden, and the page structure collapses.

    In other words, $hierarchical needs to default to 1 if &include= is not specified, and default to 0 if &include= is specified. But now an explicit specification gets discarded.

    One way to solve this is change line 1146 to be:

    if ($hierarchical == 1) $hierarchical = false;

    and line 1126, make the default "2" (so an explicit "1" won't override):

    'sort_column' => 'post_title', 'hierarchical' => 2,

    Perhaps there's a more elegant way of making a "dependent default" but this makes all cases work.

    -- \\/illiam

  26. acer135
    Member
    Posted 6 years ago #

    I'm trying to create a sub page. I don't even know where to start. help!!

  27. rudolf45
    Member
    Posted 6 years ago #

    I'd start with reading the documentation. It always helps me... Pages
    especially: this section

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags