Grow your CSS skills. Land your dream job.

[Solved] WordPress child/parent relationship

  • # June 17, 2009 at 11:16 pm

    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:

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

    # June 18, 2009 at 2:30 am

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

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

      < ?php echo $children; ?>

    < ?php } ?>

    Clicky for source

    # June 18, 2009 at 9:00 am

    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!

    # June 18, 2009 at 9:28 am

    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.

    # June 22, 2009 at 3:11 pm

    GOT IT!!! IT WORKED!!!!

    # August 6, 2009 at 1:30 am

    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:

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

    < ?php the_title(); ?>

      < ?php echo $children; ?>

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

    # August 7, 2009 at 12:28 pm

    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.

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

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

      < ?php echo $children; ?>

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

    # August 7, 2009 at 1:28 pm

    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.

    # August 7, 2009 at 2:24 pm

    Thank you…
    PLEASE let me know if you come up w/ something….
    Maybe a "parent_title" ?

    # August 7, 2009 at 2:29 pm

    Ok…. GOT IT!!!!!!!!!!!!!!!!!!

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

    I just took out the "if" statement…

    # April 22, 2011 at 12:02 pm

    What about if you wanted to make the list have a dynamic ‘current’ class?

    Thanks in advanced!

    # August 9, 2012 at 7:18 am

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



    < ?php } ?>


    # August 9, 2012 at 7:19 am

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



    < ?php } ?>

    Thanks
    Shovan

Viewing 14 posts - 1 through 14 (of 14 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".