【发布时间】:2018-03-05 16:34:23
【问题描述】:
所以我想将两个单独的模板应用于两种不同类型的页面(不是帖子)。 两种页面类型都是子页面。
模板“exhibition-template.php”应用于“归档”页面的所有子页面。 这行得通! 我使用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' );
}
//////////////////////////////////////////////
到底发生了什么?!?!? 非常感谢!
【问题讨论】:
-
好的,您可以尝试将页面的存档名称更改为另一个名称吗?喜欢“测试页”??
-
我试过了,没有运气。我觉得问题可能出在我 pages.php 文件中的 if 语句的顺序上,重新排列“else ifs”会导致它中断,
标签: php wordpress function templates