简体   繁体   中英

Only show link if the page is a child of a child in WordPress

Hay guys, i have a general navigation which looks like this:

PAGE 1
    PAGE 1.1
        PAGE 1.1.1
        PAGE 1.1.2
    PAGE 1.2
        PAGE 1.2.1

I want to display a link on pages 1.1.1, 1.1.2 and 1.2.1. As you can see these are childrens' children of the main navigation.

How would i do this in WordPress?

Use following code snippet inside the loop to achieve this objective:

<?php
if ($post->post_parent) {
   $parent = get_page($post->post_parent);
   $parentLink = get_permalink($parent->ID);

   if ($parent->post_parent) {
      $grandParent = get_page($parent->post_parent);
      $grandParentLink = get_permalink($grandParent->ID);
   }
}

// display the links to parent and grand parent pages now
echo $parentLink . ' | ' . $grandParentLink;
?>

how about just using:

<?php if( is_page('PAGENAME') ) { ?>
   <a href="#">Your Link</a>
<?php } ?>

then the link will only be shown on that page...if you want it on a few different pages then:

<?php if( is_page('PAGENAME') || 
          is_page('OTHERPAGE') || 
          is_page('ANOTHERPAGE') 
        ) { ?>
   <a href="#">Your Link</a>
<?php } ?>

this normaly works ok for me for what i need it to do..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM