自定义帖子类型的自定义分类模板已损坏

时间:2015-06-15 作者:user715564

我正在尝试为自定义分类法设置自定义模板。当我单击一个类别链接以显示该特定类别中的自定义帖子时,我会看到404页面。我的自定义帖子类型文件名是模板功能。php和我的分类归档文件是归档特性。php。下面是我注册分类法和帖子类型的代码:

自定义帖子类型和分类:

add_action( \'init\', \'post_type_features\', 0 );

function post_type_features() {

    $labels = array(
        \'name\'                => _x( \'Features\', \'Post Type General Name\', \'text_domain\' ),
        \'singular_name\'       => _x( \'Feature\', \'Post Type Singular Name\', \'text_domain\' ),
        \'menu_name\'           => __( \'Features\', \'text_domain\' ),
        \'parent_item_colon\'   => __( \'Parent Feature:\', \'text_domain\' ),
        \'all_items\'           => __( \'All Features\', \'text_domain\' ),
        \'view_item\'           => __( \'View Feature\', \'text_domain\' ),
        \'add_new_item\'        => __( \'Add New Feature\', \'text_domain\' ),
        \'add_new\'             => __( \'Add New\', \'text_domain\' ),
        \'edit_item\'           => __( \'Edit Feature\', \'text_domain\' ),
        \'update_item\'         => __( \'Update Feature\', \'text_domain\' ),
        \'search_items\'        => __( \'Search Features\', \'text_domain\' ),
        \'not_found\'           => __( \'Not found\', \'text_domain\' ),
        \'not_found_in_trash\'  => __( \'Not found in Trash\', \'text_domain\' ),
    );
    $args = array(
        \'label\'               => __( \'post_features\', \'text_domain\' ),
        \'description\'         => __( \'White Glove Labs Features\', \'text_domain\' ),
        \'labels\'              => $labels,
        \'supports\'            => array( \'title\', \'editor\', \'thumbnail\', \'custom-fields\', ),
        \'taxonomies\'          => array( \'Features\' ),
        \'hierarchical\'        => false,
        \'public\'              => true,
        \'show_ui\'             => true,
        \'show_in_menu\'        => true,
        \'show_in_nav_menus\'   => true,
        \'show_in_admin_bar\'   => true,
        \'menu_icon\'           => \'dashicons-star-empty\',
        \'can_export\'          => true,
        \'has_archive\'         => true,
        \'exclude_from_search\' => false,
        \'publicly_queryable\'  => true,
        \'capability_type\'     => \'page\',
    );
    register_post_type( \'post_features\', $args );

}

add_action( \'init\', \'lv_features_taxonomy\', 0 );

function lv_features_taxonomy() {

    $labels = array(
    \'name\' => _x( \'Features\', \'Features Taxonomy\' ),
    \'singular_name\' => _x( \'Feature\', \'Feature singular name\' ),
    \'search_items\' =>  __( \'Search Features\' ),
    \'all_items\' => __( \'All Features\' ),
    \'parent_item\' => __( \'Parent Feature\' ),
    \'parent_item_colon\' => __( \'Parent Feature:\' ),
    \'edit_item\' => __( \'Edit Feature\' ), 
    \'update_item\' => __( \'Update Feature\' ),
    \'add_new_item\' => __( \'Add New Feature\' ),
    \'new_item_name\' => __( \'New Feature Name\' ),
    \'menu_name\' => __( \'Features Categories\' ),
    );  

    register_taxonomy(\'features\',array(\'post_features\'), array(
    \'hierarchical\' => true,
    \'labels\' => $labels,
    \'show_ui\' => true,
    \'show_admin_column\' => true,
    \'query_var\' => true,
    \'rewrite\' => array( \'slug\' => \'features\' ),
    ));

}
只是为了测试,我有一个非常基本的自定义归档文件。代码如下:

    <?php get_header(); ?>
<div id="main-content" class="main-content"> 
    <div id="primary" class="content-area">
        <div id="content" class="site-content" role="main">
            <header class="archive-header">
                <h1 class="archive-title">
                    <?php post_type_archive_title(); ?>
                </h1>
            </header>           
        </div>
    </div>
</div>
<?php
get_sidebar();
get_footer();

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

根据post类型和自定义分类的代码,您的CPT是post_features 分类法是features.

对于单个post_features, 您需要将文件命名为single-post_features.php. 对于存档,创建文件taxonomy-features.php.

有关详细信息,请参阅文档。https://codex.wordpress.org/Post_Type_Templates

Example for single(single-post_features.php):

<?php get_header(); ?>
<?php
if ( have_posts() ) {
  while ( have_posts() ) {
    the_post();

    the_title();
    the_content();
  } // end while
} // end if
?>
<?php get_footer(); ?>

Example for Archive(taxonomy-features.php):

<?php get_header(); ?>
<?php
if ( have_posts() ) {
  the_archive_title( \'<h1>\', \'</h1>\' );
  while ( have_posts() ) {
    the_post();
    the_title();
    the_content();
  } // end while
} // end if
?>
<?php get_footer(); ?>
Note: 如果出现404错误,请刷新永久链接。

SO网友:Hareesh Sivasubramanian

你记得吗flush the rewrite rules 创建自定义帖子类型后?访问设置>永久链接并点击保存。(无需更改任何内容)

结束

相关推荐