Post type no single page

时间:2014-06-26 作者:Binney C

我已经创建了一个post类型,我正在尝试为该post类型创建一个页面,我正在创建一个名为single-job.php 它正在使用index.php 作为模板。下面是我创建帖子类型的函数:

add_action( \'init\', \'register_cpt_job\' );
function register_cpt_job() {

    $labels = array( 
        \'name\' => _x( \'Job posts\', \'job\' ),
        \'singular_name\' => _x( \'Job post\', \'job\' ),
        \'add_new\' => _x( \'Add New\', \'job\' ),
        \'add_new_item\' => _x( \'Add New Job post\', \'job\' ),
        \'edit_item\' => _x( \'Edit Job post\', \'job\' ),
        \'new_item\' => _x( \'New Job post\', \'job\' ),
        \'view_item\' => _x( \'View Job post\', \'job\' ),
        \'search_items\' => _x( \'Search Job posts\', \'job\' ),
        \'not_found\' => _x( \'No job posts found\', \'job\' ),
        \'not_found_in_trash\' => _x( \'No job posts found in Trash\', \'job\' ),
        \'parent_item_colon\' => _x( \'Parent Job post:\', \'job\' ),
        \'menu_name\' => _x( \'Job posts\', \'job\' ),
    );

    $args = array( 
        \'labels\' => $labels,
        \'hierarchical\' => true,

        \'supports\' => array( \'title\', \'editor\' ),
        \'taxonomies\' => array( \'category\' ),
        \'public\' => true,
        \'show_ui\' => true,
        \'show_in_menu\' => true,


        \'show_in_nav_menus\' => true,
        \'publicly_queryable\' => true,
        \'exclude_from_search\' => false,
        \'has_archive\' => true,
        \'query_var\' => true,
        \'can_export\' => true,
        \'rewrite\' => true,
        \'capability_type\' => \'post\'
    );

    register_post_type( \'job\', $args );
}

1 个回复
最合适的回答,由SO网友:Pieter Goosen 整理而成

首先,在开始之前,只需重新刷新永久链接即可。有时很奇怪为什么Template Hierarchy 失败。您的代码可以签出,理论上应该可以工作。一切都检查出来了,为了确保这一点,我对它进行了测试。

您可以使用此变通方法“强制”wordpress使用模板。在这里,我们将使用template_include 过滤器挂钩。

<?php
function wpse_template_include( $original_template ) {
    if ( isset( $wp->query_vars[\'job\'] ) && false == $wp->query_vars[\'job\']  ) {
        return get_template_directory() . \'/single-job.php\';
    } else {
        return $original_template;
    }
}

add_filter( \'template_include\', \'wpse_template_include\' );

结束