我无法访问自定义分类页面列表

时间:2014-02-28 作者:asaunders

我对使用相同URL的自定义分类法和自定义帖子类型有问题。

我的帖子类型使用的URL是/news,然后我有一个分类法,我需要有/news/cars

我跟随这篇文章来获得我的URL,但现在我无法访问实际的分类列表页面,因为我得到了404。

Mixing custom post type and taxonomy rewrite structures?

这是我用来实现此功能的代码:

    function register_custom_post_types() {
    $args = array(
        \'labels\' => array(
            \'name\' => __( \'News\' ),
            \'singular_name\' => __( \'News\' )
        ),
        \'menu_position\' => 5,
        \'public\' => true,
    \'query_var\' => \'news\',
        \'has_archive\' => \'news\',
        \'hierarchical\' => true,
        \'taxonomies\' => array(\'news_categories\'),
    \'publicly_queryable\' => true,
        \'capability_type\' => \'post\',
        \'rewrite\' => array(\'with_front\' => false, \'slug\' => \'news/%news_categories%\'),
        \'supports\' => array(\'title\', \'editor\', \'thumbnail\', \'revisions\', \'page-attributes\')
    );
    register_post_type( \'news\', $args);

}
add_action( \'init\', \'register_custom_post_types\' );

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

    function create_my_taxonomies() {
        register_taxonomy(
            \'news_categories\',
            \'news\',
            array(
                \'labels\' => array(
                    \'name\' => \'News Categories\',
                    \'add_new_item\' => \'Add New Category\',
                    \'new_item_name\' => "New Category"
                ),
                \'query_var\' => \'news\',
                \'show_ui\' => true,
                \'show_tagcloud\' => false,
                \'hierarchical\' => true,
                \'rewrite\' => array(
                    \'with_front\' => false,
                    \'slug\' => \'news\'
                )
            )
        );
    }

    add_filter(\'post_type_link\', \'news_term_permalink\', 10, 4);
    function news_term_permalink($post_link, $post, $leavename, $sample) {
        if ( false !== strpos( $post_link, \'%news_categories%\' ) ) {
            $glossary_letter = get_the_terms( $post->ID, \'news_categories\' );
            $post_link = str_replace( \'%news_categories%\', array_pop( $glossary_letter )->slug, $post_link );
        }
        return $post_link;
    }

1 个回复
最合适的回答,由SO网友:Sudeep K Rana 整理而成

我发现的唯一问题是\'query_var\' => \'news\' 在里面register_taxonomy(). 将其更改为\'query_var\' => true 它将开始工作。

function create_my_taxonomies() {
        register_taxonomy(
            \'news_categories\',
            \'news\',
            array(
                \'labels\' => array(
                    \'name\' => \'News Categories\',
                    \'add_new_item\' => \'Add New Category\',
                    \'new_item_name\' => "New Category"
                ),
                \'query_var\' => true,
                \'show_ui\' => true,
                \'show_tagcloud\' => false,
                \'hierarchical\' => true,
                \'rewrite\' => array(
                    \'slug\' => \'news\'
                )
            )
        );
    }
参考号:Codex Register Taxonomy

结束

相关推荐