Wordpress不会为父页面

时间:2016-03-19 15:25:53

标签: wordpress wordpress-theming

我正在开发一个WP主题,而我遇到这个让我疯狂的问题...

对于我创建的每个(父)页面,WP选择index.php作为模板。我了解WP模板层次结构,但如果我使用page.phppage-$slug.phppage-$id.php并不重要,我也尝试使用:

<?php
  /*
  Template Name: My Template 
  */
?>

并在编辑器中为此模板分配页面。

出于调试目的,我还挂了template_include如下:

add_filter( 'template_include', 'check_template_selection', 99 );

function check_template_selection( $template ) {
    global $post;
    echo "Post ID: " . $post->ID . "<br/>";
    echo "Template File:" . $template . "<br/>";
    echo is_page() ? "YES" : "NO";
    return $template;
}

对于我检查的每个网页网址(例如:http://example.com/mypage),我得到了:

  

帖子ID:

     

模板文件:/ nfs / c11 / h05 / mnt / 205383 / domains / example.com / html / wp-content / themes / mytheme /index.php

     

NO

我尝试删除这些页面并重新创建它们,但它们是一样的。事实是它适用于子页面。例如:http://example.com/mypage/mychildpage显示:

  

帖子ID:589

     

模板文件:/ nfs / c11 / h05 / mnt / 205383 / domains / example.com / html / wp-content / themes / mytheme / page-mychildpage .PHP

     

YES

我使用WP 4.4.2,ACF PRO 5.3.6.1,自定义帖子类型UI 1.2.4和Instagram Feed Pro Personal 2.0.4.1。没有其他插件。无论如何,我试图将它们全部去掉。我从头开始制作这个主题,之前我已经完成了几十个主题,而且我之前从未遇到过这个问题...

这是我的.htaccess,我的永久链接已正确配置为/%postname%/,没有类别基础。

# BEGIN WordPress

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

所以,对于我在这里失踪的任何建议或暗示都会非常渺茫。谢谢你们!

编辑:

经过几个小时的深度搜索(非常感谢John Blackbourn为您的Query Monitor插件),我发现我的父网页符合错误的重写规则,所以现在我想它是关于重写规则优先级的

父网页网址错误匹配: http://example.com/about

Wrong Match Wrong Rule Selected

子网页网址的正确匹配: http://example.com/about/about-child

Right Match Right Rule Selected

1 个答案:

答案 0 :(得分:1)

几年后,但我遇到了几乎相同的问题。像您一样使用Query Monitor插件也帮助我弄清楚了。

我今天早些时候所做的是创建一个名为year的自定义分类法。我的想法是按制造车辆的年份将我的帖子(现在是我客户的站点的车辆)分组。

但是,就像您一样,当我转到任何顶级页面时,我都被引导到index.php模板文件而不是page.php模板文件。

通过使用Query Monitor插件,我发现我得到的结果与您完全相同。我匹配的查询是year=about

我删除了自定义year分类法,然后通过WordPress管理员Settings > Permalinks和voila保存了永久链接,从而刷新了我的永久链接,现在我的顶层页面将转到正确的page.php模板文件

相关问题