WordPress.org

Ready to get started?Download WordPress

Forums

Get title for Page parent (15 posts)

  1. trubble
    Member
    Posted 7 years ago #

    Hi

    I have a number of Pages that have been setup to have Page Parents. I have split my navigation into a main and subnav no problem, but I would like to display the title (wp_title) of the parent Page above my subnav. Is there a way to do this?

    I have had a look around the site but can't find how to tell it to get the title one level up, if you see what I mean.

    Thanks in advance!!

  2. dtrain
    Member
    Posted 7 years ago #

    I would also love to know how to do this. I am using wordpress as a cms and my client needs the child pages to display the parent title as the header at the top of every page and the current page title on a new line (only if the page has a parent).

    Please help!!!

  3. joost538
    Member
    Posted 7 years ago #

    It's actually quite simple. To display a link to the parent page:

    <?php wp_list_pages('title_li=&include='.$post->post_parent) ?>

    Unfortunately, this will always display an html list item (li) which is likely not what you want. Thus it gets a little nastier, using php search and replace.

    <?php if(get_the_title($post->post_parent) != the_title(' ' , ' ',false)) {
    	$parent = wp_list_pages('echo=0&title_li=&include='.$post->post_parent);
    	$parent = str_replace('<li class="page_item current_page_parent">', '', $parent);
    	$parent = str_replace('</li>', '', $parent);
    	echo "&laquo; back to $parent";
    } ?>
  4. sadish
    Member
    Posted 7 years ago #

    I think this should be done in the wordpress core itself. anyone can take this to the developers ?

  5. charmed
    Member
    Posted 7 years ago #

    @joost538, you are a lifesaver! I've been searching how to do this for 2 days now, and your code was the answer.. Using wp as a CMS for my design portfolio site. See it in action to display the parent category in the sidebar box on the right here:
    http://www.fallenskydesign.com/services/html-email-design/ (or any of the sub pages)
    Thank you again!!

    PS, Sadish, I LOVE your misty look theme. I use it as a starting point for all my blog designs :)

  6. Raquel Wilson
    Member
    Posted 7 years ago #

    joost538:

    on the code you listed above for displaying just the parent title on a child page

    <?php wp_list_pages('title_li=&include='.$post->post_parent) ?>

    I found some info on how to remove the list item styling on this page: http://codex.wordpress.org/Template_Tags/wp_list_pages

    The parent title list can be styled in your CSS (to remove the bullet point) by using the following class selectors:

    .page_item { ... }
    .current_page_item { ... }

    I hope this is clear. If not just check out the page I listed above. It worked for me.

    BTW -- thanks for the code!

  7. sojweb
    Member
    Posted 6 years ago #

    That's a roundabout way of doing it; try this:

    if(intval($post->post_parent)>0)
    {
    	while(intval($post->post_parent)>0)
    		$post = get_post($post->post_parent);
    }
    echo '<a href="'.get_permalink($post->post_parent).'">'.get_the_title($post->post_parent).'</a>';
  8. sojweb
    Member
    Posted 6 years ago #

    Sorry, that last line should read:

    echo '<a href="'.get_permalink($post->post_parent).'">'.get_the_title($post->post_parent).'</a>';
  9. sojweb
    Member
    Posted 6 years ago #

    One more try:

    echo '<a href="'.get_permalink($post->ID).'">'.get_the_title($post->ID).'</a>';

  10. jazbek
    Member
    Posted 6 years ago #

    sojweb, that snippet really helped me out. thanks a bunch!

  11. vossavant
    Member
    Posted 6 years ago #

    Going off of sojweb's last bit of code, I came up with:

    echo '<a href="'.get_permalink($post->post_parent).'">'.get_the_title($post->post_parent).'</a>';

    This does the trick of displaying the parent page's title if you are on a subpage of the parent or on the parent itself.

    Alternatively, you can make it a heading by swapping in <h2> tags for the <a> tags.

  12. vossavant
    Member
    Posted 6 years ago #

    In that last bit of code, the only change is from

    $post->ID

    to

    $post->post_parent

  13. superted
    Member
    Posted 6 years ago #

    Great job. I was luck to find this support string. Here's what I assembled after playing around for a while in the forum.

    - displays parent page title as the list header whether on the parent or child page
    - if on the child page, it makes the parent's title into a hyperlink so visitors can get back to the parent page
    - returns html that will work with your style sheet to look exactly like a normal wp_list_pages call

    [Code Moderated]
    [Use Code tag when posting Code]

    see a version at http://powermyhealth.com (as of 11-16-07)

  14. Martensen
    Member
    Posted 6 years ago #

    fu**ing great moderation :(

    how about fixing it instead of deleting it?
    that would certainly help a lil bit more,
    or is this just about showing muscles?

  15. jazbek
    Member
    Posted 6 years ago #

    if they just fixed it for you, you would have no incentive to be sure to use the right markup on future posts.. thus creating even more work for the moderators, who i'm sure have their hands full.

Topic Closed

This topic has been closed to new replies.

About this Topic