未显示自定义分类模板

时间:2017-08-22 作者:jardindeden

我想显示分类法模板时遇到问题
我已经为我的自定义帖子类型“Project”创建了一个自定义分类“Location”。此外,我还为其创建了以下模板:

taxonomy-{taxonomy_name}-{term}.php
taxonomy-{taxonomy_name}.php
taxonomy.php
它们都不起作用。。。我刚得到我的存档{post\\u type}。php在所有情况下。

我的目标URL是:
mywebsite.com/holliday/united-kingdom/ (位置分类法)
mywebsite.com/holliday/united-kingdom/london (位置分类子项)

在创建分类法时,我本应该把事情搞砸或遗漏一些东西,但我做不好。

这是我的代码(register post type和register taxonomy):

// Register Post Type (Holliday)
$capability = \'publish_posts\';

$cpt_args = array(
    \'menu_icon\'         => \'dashicons-groups\',
    \'labels\'            => array(
        \'name\'              => __( \'Holliday\', \'mon-plugin\' ),
        \'singular_name\'     => __( \'Holliday\', \'mon-plugin\' ),
        \'add_new\'           => __( \'Add Holliday\', \'mon-plugin\' ),
        \'add_new_item\'      => __( \'Add Holliday\', \'mon-plugin\' ),
        \'edit\'              => __( \'Edit\', \'mon-plugin\' ),
        \'edit_item\'         => __( \'Edit Holliday\', \'mon-plugin\' ),
        \'new_item\'          => __( \'New Holliday\', \'mon-plugin\' ),
        \'view\'              => __( \'View Holliday\', \'mon-plugin\' ),
        \'view_item\'         => __( \'View Holliday\', \'mon-plugin\' ),
        \'search_items\'      => __( \'Search Holliday\', \'mon-plugin\' ),
        \'not_found\'         => __( \'No Holliday found\', \'mon-plugin\' ),
        \'not_found_in_trash\'=> __( \'No Holliday found in Trash\', \'mon-plugin\' ),
        \'parent\'            => __( \'Parent Holliday\', \'mon-plugin\' ),
    ),
    \'public\'            => true,
    \'has_archive\'       => true,
    \'rewrite\'           => array(
        \'slug\'              => \'holliday/%my_project_location%\',
    ),
    \'supports\'          => array(\'title\', \'custom-fields\', \'excerpt\', \'editor\', \'author\', \'thumbnail\', \'comments\'),
    \'capabilities\'      => array(
        \'publish_posts\'     => $capability,
        \'edit_posts\'        => $capability,
        \'edit_others_posts\' => $capability,
        \'delete_posts\'      => $capability,
        \'delete_others_posts\'=> $capability,
        \'read_private_posts\'=> $capability,
        \'edit_post\'         => $capability,
        \'delete_post\'       => $capability,
        \'read_post\'         => $capability
    ),
);
register_post_type( \'my_project\', $cpt_args );

// Register Taxonomy (Location)
$cats_args = array( 
    \'labels\'            => array(
        \'name\'              => _x( \'Project Location\', \'mon-plugin\' ),
        \'singular_name\'     => _x( \'Location\', \'mon-plugin\' ),
        \'search_items\'      => __( \'Search Location\', \'mon-plugin\' ),
        \'all_items\'         => __( \'All Location\', \'mon-plugin\' ),
        \'parent_item\'       => __( \'Parent Location\', \'mon-plugin\' ),
        \'parent_item_colon\' => __( \'Parent Location:\', \'mon-plugin\' ),
        \'edit_item\'         => __( \'Edit Location\', \'mon-plugin\' ),
        \'update_item\'       => __( \'Update Location\', \'mon-plugin\' ),
        \'add_new_item\'      => __( \'Add New Location\', \'mon-plugin\' ),
        \'new_item_name\'     => __( \'New Location Name\', \'mon-plugin\' ),
        \'menu_name\'         => __( \'Location\', \'mon-plugin\' ),
    ),
    \'hierarchical\'      => true,
    \'public\'            => true,
    \'show_ui\'           => true,
    \'has_archive\'       => true,
    \'rewrite\'           => array(
        \'slug\'              => \'holliday\',
        \'hierarchical\'      => true
    )
);
register_taxonomy( \'my_project_location\', \'my_project\', $cats_args );
编辑:
如果我将分类法slug更改为其他内容,我会使分类法模板正常工作
示例:

 taxonomy slug => "location"  
 mywebsite.com/location/united-kingdom/ => Taxonomy template working !  
但我希望我的分类法与我的自定义帖子类型配合使用。访问时获取分类法模板mywebsite.com/{custom-post-type}/{taxonomy}/<但是这个给我archive-{custom-post-type}.php 改为文件。

遵循Template Hierarchy 我希望Wordpress在归档模板之前为我找到分类模板。

我有什么遗漏吗<谢谢你的帮助。

2 个回复
最合适的回答,由SO网友:jardindeden 整理而成

感谢WebElaine的帮助!我想出了一个解决办法。

看起来有点像你在Option 2. 除了我用了过滤器parse_query 更改我的CPT\'has_archive\' 访问我的分类时为false。

代码如下:

function check_taxonomy_query( $query ) {
    if ($query->query[\'my_project_location\'] && !$query->query[\'my_project\']) {
        $query->is_post_type_archive = false;
    }
}
add_action( \'parse_query\', \'check_taxonomy_query\' );  
my_project_locationmy_project 查询变量是否用于我的重写规则:

$newRules[\'holliday/?$\']    =   \'index.php?lang=en&post_type=my_project\';
$newRules[\'holliday/([^/]+)/?$\']    =   \'index.php?lang=en&post_type=my_project&my_project_location=$matches[1]\';
$newRules[\'holliday/([^/]+)/([^/]+)/?$\']    =   \'index.php?lang=en&post_type=my_project&my_project_location=$matches[1]/$matches[2]\';
$newRules[\'holliday/([^/]+)/([^/]+)/([^/]+)/?$\']    =   \'index.php?lang=en&post_type=my_project&my_project_location=$matches[1]/$matches[2]&my_project=$matches[3]\';
所以在check_taxonomy_query 函数,我只需检查我是否处于重写规则的第2行或第3行。如果是,我将关闭post type存档。

希望它能帮助别人;)

SO网友:WebElaine

如果您只想拥有/holliday/project location/URL,而不想拥有单独的归档和分类URL,那么有几种不同的解决方案。

Option 1:

使用template_include() filter. 我认为此代码可以工作,但尚未测试:

add_filter( \'template_include\', \'holliday_template\', 99 );

function holliday_template( $template ) {

    if ( is_post_type_archive( \'holliday\' )  ) {
        $new_template = locate_template( array( \'taxonomy-my_project_location.php\' ) );
        if ( \'\' != $new_template ) {
            return $new_template;
        }
    }

    return $template;
}
Option 2 (更简单):

告诉WP不要holliday CPT存档。注册帖子类型时,只需更改\'has_archive\' => true,false. 这样,当WP在您想要的URL上查找内容时,只有分类法归档文件存在。您很可能需要使用unregister_post_type() 并重新注册以强制更改。

结束

相关推荐