• trubble

    (@trubble)


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

Viewing 14 replies - 1 through 14 (of 14 total)
  • 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!!!

    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";
    } ?>

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

    @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:
    https://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 ??

    Raquel Wilson

    (@belindasuperstarr)

    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: https://codex.www.ads-software.com/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!

    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>';

    Sorry, that last line should read:

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

    One more try:

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

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

    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.

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

    $post->ID

    to

    $post->post_parent

    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 https://powermyhealth.com (as of 11-16-07)

    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?

    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.

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘Get title for Page parent’ is closed to new replies.