【问题标题】:Wordpress - apply template page to two separate sets of child pagesWordpress - 将模板页面应用于两组单独的子页面
【发布时间】: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


【解决方案1】:

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

 } 
 ?>

【讨论】:

  • 谢谢,但这不起作用。或者我不确定你的意思。
  • 你能再试一次吗?如果您有任何问题,只需删除您在 page.php 中的任何其他不必要的代码即可找到您的问题。当然你会先备份你的文件!祝你好运
  • 我现在看到将该代码添加到我的 pages.php 中,将添加“展览模板”,但它位于页面的其余部分,如 iframe:78.media.tumblr.com/acf87b0054a9b6fcd300cd6e48e09a3c/… 整个页面应该是白色,但看起来像这样
  • 只需从您的 page.php 中删除除此代码和 wp_head、wp_footer 之外的所有内容,以查看发生了什么。但这就是您问题背后的逻辑;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-08
  • 2012-10-08
相关资源
最近更新 更多