简体   繁体   中英

Display parent page only on sub-page wordpress

I want to display parent page only on sub-pages. I am using this code but it display parent page on every page.

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

you should try this function to check the parent child relation

function is_child($pageID) { 
global $post; 
if( is_page() && ($post->post_parent==$pageID) ) {
           return true;
} else { 
           return false; 
}
}

and use it like that

if(is_child(123)) { //parent id
  $parent_title = get_the_title($post->post_parent);
  echo $parent_title;
 }

you can try this

<?php 
if($post->post_parent>0)
{
$parent_title = get_the_title($post->post_parent);
echo $parent_title;
}
?>

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