Wordpress - 将模板页面应用于两组独立的子页面

问题描述 投票:0回答:1

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

模板'exhibition-template.php'适用于'archive'页面的子页面。这有效!我使用functions.php中的函数执行此操作,该函数测试页面是否为'archive'的子项,然后在page.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;
}
}
//////////////////////////////////////////////

并在page.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' );
}
//////////////////////////////////////////////

问题是当我第二次使用新变量编写函数时,尝试以完全相同的方式实现它,除了将'artist-template.php'应用于'艺术家'的子页面,没有任何变化,实际上它仍然无论如何应用'exhibition-template.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' );
}
//////////////////////////////////////////////

Whaaaaat到底是怎么回事?!?!?非常感谢!

php wordpress function templates
1个回答
0
投票

你可以在上面的page.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

 } 
 ?>
© www.soinside.com 2019 - 2024. All rights reserved.