treehouse : what would you like to learn today?
Web Design Web Development iOS Development

[Solved] Wordpress child/parent relationship

  • Disclaimer: I know this isn't a Wordpress site, but I'm posting this in the PHP section because my question may require a some advanced php scripting... and Wordpress. :D

    ok so I'm wanting to design a site that will have children to parent pages. The normal nav will be in a "index_sidebar.php".
    On top of that, I will want the links of the children to appear on a "inside_sidebar.php". (inside "pages" will look different than the

    To further explain my, I've created a framework.
    Here's a http://img200.imageshack.us/i/capturerwb.jpg/ of what I'm trying to do.
    The blue is the parent nav and the red is WHERE the children nav will be (ignore the stuff that is there now).

    Only 3 pages have children, so I'm wondering if there's a cool/easy WP-php tag I could throw where the children nav would be that would ask the parent nav:
    if there's children, show here; if not, show nothing and allow the 'recent comment' box to move up in place


    It may be a stretch asking for that, but I thought check.
    Any help would be MUCH appreciated.

    -Eric
  • The WordPress Codex is your best friend ever!

    "This code will show the child pages, and only the child pages, when on a parent or on one of the children. This code will not work if placed after a widget block in the sidebar."

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


    Clicky for source
  • I wasn't planning on placing it in a widget block... am i good to go w/ this you think?
    I'll try it out today, if I have a chance.
    THANK YOU!
  • I just checked an old client site and this was exactly the code I used. Wordpress will even throw in the classes for you for highlighting current pages etc.
    So yes, you shouldn't have any problems.
  • GOT IT!!! IT WORKED!!!!
  • Ok, so the code provided in this thread is AWESOME!

    But....

    The title of the parent page is now what I'm after..

    Here's my current code:

    <?php
    if($post->post_parent)
    $children = wp_list_pages(\"title_li=&child_of=\".$post->post_parent.\"&echo=0\");
    else
    $children = wp_list_pages(\"title_li=&child_of=\".$post->ID.\"&echo=0\");
    if ($children) { ?>
    <div class=\"summer_side\">
    <h3 class=\"child-nav\"><?php the_title(); ?></h3>
    <ul>
    <?php echo $children; ?>
    </ul>
    </div>
    <?php } ?>


    My problem is <?php the_title(); ?>. I want the title of the parent to show instead of the child. This way when choosing a child, the parent title exists the same in the H3 that i have there.


    I know there's gotta be something like post_parent_title, but I'm just not sure.

    Any ideas? Help!

    Thanks!
  • Ok... I found some solutions...

    This is the one I used....
    http://www.tammyhartdesigns.com/tutorials/wordpress-how-to-display-the-parent-page-title-in-your-title-meta-tag/

    Here's some others...
    http://wordpress.org/support/topic/270336
    http://www.tammyhartdesigns.com/tutorials/wordpress-how-to-list-child-pages-in-sidebar/
    http://www.fldtrace.com/wordpress/how-to-display-parent-page-title-in-wordpress
  • Ok... another problem.

    Whem I'm on the parent, I want the parent title to show up in my nav, but it's not.
    This is the code I'm using.

    <?php
    if($post->post_parent)
    $children = wp_list_pages(\"title_li=&child_of=\".$post->post_parent.\"&echo=0\");
    else
    $children = wp_list_pages(\"title_li=&child_of=\".$post->ID.\"&echo=0\");
    if ($children) { ?>
    <div class=\"summer_side\">
    <h3 class=\"child-nav\">
    <?php
    if($post->post_parent) {
    $parent_title = get_the_title($post->post_parent);
    echo $parent_title;
    ?><?php } ?>
    </h3>
    <ul>
    <?php echo $children; ?>
    </ul>
    </div>
    <?php } ?>


    Here's a link to the site itself... http://www.ericdmunoz.com/dhe/summer-programs/

    You'll notice that there's a spot for an h3, but it won't show up until you click on a child.

    Any ideas how to have my cake and eat it, too?
  • I dig the site! Looks great.

    I was just about to post some code, but then realized you already had it in there. It has to be something very simple, I feel like I'm overlooking something.
  • Thank you...
    PLEASE let me know if you come up w/ something....
    Maybe a "parent_title" ?
  • Ok.... GOT IT!!!!!!!!!!!!!!!!!!


    <?php
    $parent_title = get_the_title($post->post_parent);
    echo $parent_title;
    ?>


    I just took out the "if" statement...
  • What about if you wanted to make the list have a dynamic 'current' class?

    Thanks in advanced!
  • How do you get parent page url? This is what I have

    <?php
    if($post->post_parent) {
    $children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
    $titlenamer = get_the_title($post->post_parent);
    }

    else {
    $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
    $titlenamer = get_the_title($post->ID);
    }
    if ($children) { ?>

    <ul class="tabs vertical hide-on-phones three columns">
    <li id="title-parent"><a href="PARENT-URL"><?php echo $titlenamer; ?></a></li>
    <?php echo $children; ?>
    </ul>

    <?php } ?>


  • Found the solution :)


    <?php
    if($post->post_parent) {
    $children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
    $titlenamer = get_the_title($post->post_parent);
    }

    else {
    $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
    $titlenamer = get_the_title($post->ID);
    }
    if ($children) { ?>

    <ul class="tabs vertical hide-on-phones three columns">
    <li id="title-parent"><a href="<?php echo get_permalink($post->post_parent) ?>"><?php echo $titlenamer; ?></a></li>
    <?php echo $children; ?>
    </ul>

    <?php } ?>


    Thanks
    Shovan