开发者

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜