WordPress.org

Ready to get started?Download WordPress

Forums

Displaying Parent name in sidebar (7 posts)

  1. vivacitydesign
    Member
    Posted 6 years ago #

    I've set up my template to display links to the Child Pages of a Parent in the sidebar - but only when you are viewing that Parent. At the moment I'm using the following code:

    <ul class="block">
    						<?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 } ?>
    					</ul>

    And everything is working fine...

    But, I would like to display the title of the Parent page above these links to the Children...

    I'm new to PHP and I'm still trying to get my head around it...I understand enough to know that the top part of the PHP code is determining whether or not the page is a child, and that the part on the bottom calls the function to either display the child links, or not. I'm just unclear where to add a snippet of code to basically say:

    IF child page THEN display PARENT-TITLE above child links in h2

    Any help would be greatly appreciated!

  2. stanch
    Member
    Posted 6 years ago #

    have you try using the pithy code something like this?:

    <li>
    <h2 class="sidebartitle"><?php _e('Categories'); ?></h2>
    <ul class="list-cat">
    <?php wp_list_cats('sort_column=name&optioncount=1&hierarchical=1'); ?>
    </ul>
    </li>
  3. vivacitydesign
    Member
    Posted 6 years ago #

    You'll have to forgive my naivety, but I'm not sure how to use that piece of code to help me. It looks like it's just going to display a list of Categories...there's no conditional codes in there.

    I clarify:

    I have a Parent Page with three Child Pages

    Procedures (PARENT)

    Procedure 1 (CHILD)
    Procedure 2 (CHILD)
    Procedure 3 (CHILD)

    At the moment, I have sidebar setup to display the list of child pages - but ONLY IF the page that is currently being displayed is a PARENT or CHILD that are RELATED.

    I would like to display the RELATED PARENT TITLE above the list of CHILD links.

    Is there a way to do this, whilst still maintaining the conditional nature of the child link display? Furthermore, can I make the title that does appear conditional to which particular parent/child set is being looked at?

    Apologies for the capitals - just trying to make it make more sense.

  4. vivacitydesign
    Member
    Posted 6 years ago #

    Nevermind. I worked it out!

  5. vivacitydesign
    Member
    Posted 6 years ago #

    Ok, so I worked out how to do what I wanted by changing the code to this:

    <?php
    if($post->post_parent)
    $children = wp_list_pages('title_li=&child_of='.$post->post_parent.'&echo=0'); else
    $children = wp_list_pages('exclude=13&title_li=<h2>Additional Links</h2>&child_of='.$post->ID.'&echo=0');
    if ($children) { ?>
    
    <ul>
    <?php echo $children; ?>
    
    </ul>

    But now I'm having another problem!

    This is my page structure:

    TEAMS (PARENT)
    - SENIOR (CHILD of TEAMS)
    - PLAYER PROFILE 1 (CHILD of SENIOR)
    - PLAYER PROFILE 2 (CHILD of SENIOR)
    - PLAYER PROFILE 3 (CHILD of SENIOR)
    ETC
    - JUNIOR (CHILD of TEAMS)
    - PLAYER PROFILE 1 (CHILD of SENIOR)
    - PLAYER PROFILE 2 (CHILD of SENIOR)
    - PLAYER PROFILE 3 (CHILD of SENIOR)
    ETC

    Using the code I posted above, I can only use the wp_list_pages('exclude=xx'); on the first parent page (in this case 'TEAMS'), and it doesn't work with any pages lower in the hierarchy.

    This means that when you're looking at the TEAMS page (first PARENT), only the direct children appear in the sidebar (because I've manually excluded the other PLAYER PROFILES using the wp_list_pages exclude tag). But when you open one of the child pages (JUNIOR or SENIOR), all children of TEAMS display in the sidebar.

    Can anyone think of a solution to this? I would very much appreciate any feedback!

  6. vivacitydesign
    Member
    Posted 6 years ago #

    bump...

  7. joncalex
    Member
    Posted 5 years ago #

    Couldnt you use this to display the post parent? if the parent is not a child of any page, then that title displays instead.

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

Topic Closed

This topic has been closed to new replies.

About this Topic