WordPress - 将模板页面应用于两组单独的子页面



所以我想将两个单独的模板应用于两种不同类型的页面(不是帖子(。这两种页面类型都是子页面。

模板

"展览模板.php应用于作为"存档"页面子级的所有页面。这行得通!我使用函数中的函数来执行此操作.php该函数测试页面是否是"存档"的子级,然后在页面中使用该函数.php以应用模板:

//////////////////////////////////////////////
function is_archivechild() {
// Set up the objects needed
$my_wp_query = new WP_Query();
$all_wp_pages = $my_wp_query->query(array('post_type' => 'page', 'posts_per_page' => '-1'));
// Get the page as an Object
$archive = get_page_by_title('archive');
// Filter through all pages and find archive's children
$archive_children = get_page_children( $archive->ID, $all_wp_pages );
if ($archive_children > 0){
return true;
}
else{
return false;
}
}
//////////////////////////////////////////////

并在页面中使用它.php:

//////////////////////////////////////////////
elseif ( is_archivechild() ) {/*a function made up in functions.php, tests if it is a of archive*/
get_template_part( 'exhibition-template' );
get_template_part( 'normalfooter' );
}
//////////////////////////////////////////////

问题是当我使用新变量第二次编写函数时,并尝试以完全相同的方式实现它,除了将"艺术家模板.php应用于"艺术家"的子页面,没有任何变化,实际上它仍然应用"展览模板.php无论如何。

//////////////////////////////////////////////
function is_artistschild() {
// Set up the objects needed
$artist_query = new WP_Query();
$all_wp_pages = $artist_query->query(array('post_type' => 'page', 'posts_per_page' => '-1'));
// Get the page as an Object
$artists = get_page_by_title('artists');
// Filter through all pages and find artists's children
$artists_children = get_page_children( $artists->ID, $all_wp_pages );
if ($artists_children > 0){
return true;
}
else{
return false;
}
}
//////////////////////////////////////////////
elseif ( is_artistchild() ) {/*a function made up in functions.php, tests if it is a CHILD page, aka an exhibition*/
get_template_part( 'artist-template' );
get_template_part( 'normalfooter' );
}
//////////////////////////////////////////////

哇?!?!?哇哇��非常感谢!

你可以在页面内写.php上面:

<?php global $post;
      $page = get_page_by_title( 'archive' ); // page title  , Archive in your case . Try an alternative page name.
      $page_parent_ID=$page->ID; 
?>
<?php if ($post->post_parent == $page_parent_ID) {
get_template_part( 'exhibition-template' ); // you name of template you want to appear
 } 
 ?>

相关内容

最新更新