• Howdy,

    We are using WP as a lost-cost CMS solution for some intranet websites, so the site mainly consists of pages. I currently have a left-nav that consists of the parent page and child pages, that is generated by the following:

    <div id="sidebar">
    
    <?php
    if($post->post_parent)
    $children = wp_list_pages("sort_column=menu_order&amp;title_li=&amp;child_of=".$post->post_parent."&amp;echo=0"); else
    $children = wp_list_pages("sort_column=menu_order&amp;title_li=&amp;child_of=".$post->ID."&amp;echo=0");
    if ($children) { ?>
    <ul>
    <li class="top">
    <?php
    $parent_title = get_the_title($post->post_parent);
    echo $parent_title;
    ?>
    </li>
    <?php echo $children; ?>
    </ul>
    <?php } ?>

    Which is fine, except, the parent page item is not a link, so I need to make it a link.

    I tried adapting various versions of:

    <?php if($post->post_parent) {
         $parent = $wpdb->get_row("SELECT post_title FROM $wpdb->posts WHERE ID = $post->post_parent");
         $parent_link = get_permalink($post->post_parent); ?>
         <a href="<?php echo $parent_link; ?>">Back to <?php echo $parent->post_title; ?></a>
    <?php } ?>

    which I got from the forums here. And while it would sort of work, it is applying extra classes that are causing the layout to get hosed.

    What I need is a cleaner way of adding the parent page link at the top of the navigation so I can style it from my master css. (I’m applying all my styles based on the div ID, ingoring the WP classes in this case).

    Any ideas?…

Viewing 10 replies - 1 through 10 (of 10 total)
  • I don’t have a solution to your current dilemma, but if I may offer an alternative solution:

    Breadcrumb NavXT – a plugin that makes “bread crumb” navigation for your users. It would show the parent page, I am assuming at the top of the post or page.

    Thread Starter kwj

    (@kwj)

    Thanks for that tip, I am going to look at adding that for complex sites.

    We still want to fix our left-navigation, so any help getting the parent page links in the main nav would be appreciated! ??

    Hi,

    To be honest I am not really sure if I know what I am doing, but I have been after exactly the same thing. By taking your two examples I have put together this:

    <?php
    if($post->post_parent)
    $children = wp_list_pages("sort_column=menu_order&amp;title_li=&amp;child_of=".$post->post_parent."&amp;echo=0"); else
    $children = wp_list_pages("sort_column=menu_order&amp;title_li=&amp;child_of=".$post->ID."&amp;echo=0");
    if ($children) { ?>
    <ul>
    <li class="top">
    <?php
    $parent_title = get_the_title($post->post_parent);
    echo $parent_title;
    ?>
    <li><?php
    $parent_title = get_the_title($post->post_parent);?>
    <a href="<?php echo get_permalink($post->post_parent) ?>"><?php echo $parent_title;?></a>
    </li>
            <?php echo $children;?>
        <?php }?>

    As you can see all I did differently to your first code (that showed the title, but not the link) was to wrap the a tag with permalink reference.

    It does the job for me so thank you for posting!! I hope that it works for you.

    …actually I take it back. This isn’t quite doing the job!

    The code I submitted shows the immediate parent. But does anyone know how I could show the original parent – if for example I was on a grandchild page?

    For anyone interested I think that I may have cracked it. I am not a coder at all so this might be really ugly, but it does the job for me. TBH I am not sure that I really understand why it works!! I took a lot of code from many threads and kept going until it worked…

    I have a side menu where I only want to show the parent (and link to it) and all the children & grandchildren – no matter where I am within the parents family. I also wanted a link to the site’s homepage always at the top. I think it uses fairly normal theme css (depending how many levels of UL your css file has I guess)

    If anyone can make this more elegant it would be great.

    <?php
        if($post->post_parent) {
            $parent = get_post($post->post_parent);
            if ($parent->post_parent) {
                $children = wp_list_pages("depth=3&amp;sort_column=menu_order&amp;title_li=&amp;child_of=".$parent->post_parent."&amp;echo=0");
            } else {
                $children = wp_list_pages("depth=3&amp;sort_column=menu_order&amp;title_li=&amp;child_of=".$post->post_parent."&amp;echo=0");
            }
        }
        else
            $children = wp_list_pages("depth=3&amp;sort_column=menu_order&amp;title_li=&amp;child_of=".$post->ID."&amp;echo=0");
        if ($children) {?>
        <h2>In this section:</h2>
        <ul>
        <li><a href="<?php echo get_option('home'); ?>/" title="<?php bloginfo('name'); ?>">HOME</a></li>
        <li>
    
    	<?php //variables
    	$grandparent = $parent->post_parent;
    	$grandparent_title = get_the_title($grandparent);
    	$parent_title = get_the_title($parent);?>
    
    	<?php // is the page a grandchild?
    	if ($grandparent == is_page(0)) { ?>
    	<a href="/<?php echo $grandparent_title ?>"><?php echo $grandparent_title;?>	</a>
    
    	<?php // is the page a child?
    	} elseif ($post->post_parent ==is_page(0)) {?>
        <a href="<?php echo get_permalink($post->post_parent) ?>"><?php echo $parent_title;?></a>
    
        <?php // I must be this page!
    	} else { ?> <a href="<?php echo get_permalink() ?>"><?php the_title(); ?> <?php }?></a>
    
           <ul> <?php echo $children;?></ul></li>
           </ul>
    
        <?php }?>

    I hope that this is helpful to someone out there.

    Hi All,

    I tried everything with no luck until, until I found this plugin, the “Sub Pages Widget” by Alper Haytabay.

    https://www.ads-software.com/extend/plugins/subpages-widget/

    It works perfectly, and I now have the parent page linked, as well as all the features discussed above. Am using 2.7.1, so far so good.

    Hope this helps!

    If you want to display the title of the immediate parent of a subpage:

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

    If you want to display the title of the immediate parent of a subpage and link to that page, use:

    <?php
    
    $parent_title = get_the_title($post->post_parent);?>
    <a href="<?php echo get_permalink($post->post_parent) ?>"><?php echo $parent_title;?></a>
    
    <?php echo $children;?>

    @dakranii Thanks this works perfectly!

    Cheers,
    Maggy

    Here’s what I did.

    if($post->post_parent) {
    $children = wp_list_pages(“title_li=&child_of=”.$post->post_parent.”&echo=0″);
    $titlenamer = get_the_title($post->post_parent);
    $parentlink = get_permalink($post->post_parent);
    }

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

    <?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);
      $parentlink = get_permalink($post->post_parent);
      }
    
      else {
      $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
      $titlenamer = get_the_title($post->ID);
      $parentlink = get_permalink($post->ID);
      }
      if ($children) { ?>
        <ul>
          <li><a href="<? echo $parentlink; ?>"><? echo $titlenamer; ?></a></li>
          <li><?php echo $children; ?></li>
        </ul>
    
    <?php } ?>
Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Link to parent page in navigation?’ is closed to new replies.