最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

templates - If Child or Grandchild of Page

programmeradmin4浏览0评论

I'm serving a different header in Wordpress depending what section you are within the site, 'residential' , 'business' or other by using the following code. Although i need to extend it to work with grandchildren as well, so another level down.

<?php
/*
Template Name: Package
*/ 
if (is_page('business') || $post->post_parent=="17")
{
get_header('business');
}
else if (is_page('residential') || $post->post_parent=="19")
{
get_header('residential');
}
else 
{
get_header();
}
?>

I'm serving a different header in Wordpress depending what section you are within the site, 'residential' , 'business' or other by using the following code. Although i need to extend it to work with grandchildren as well, so another level down.

<?php
/*
Template Name: Package
*/ 
if (is_page('business') || $post->post_parent=="17")
{
get_header('business');
}
else if (is_page('residential') || $post->post_parent=="19")
{
get_header('residential');
}
else 
{
get_header();
}
?>
Share Improve this question edited Jun 28, 2012 at 15:17 Stephen Harris 32.6k6 gold badges84 silver badges118 bronze badges asked Jun 28, 2012 at 14:54 Roy BarberRoy Barber 1211 silver badge4 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 2

get_ancestors() will return the full parent page ID hierarchy for a given page ID which you could check against.

You can count post ancestor like this

$count = count(get_post_ancestors($post->ID));

The results means:

2= grandchild

1= child

0=parent

发布评论

评论列表(0)

  1. 暂无评论