我如何才能只显示我的自定义帖子类型的一个类别的档案?

时间:2017-03-01 作者:Jordan Davis

这与问题不同:How to display categories of my Custom Post Type?

我有一个自定义的帖子类型-播客。我添加了分类法“category”,因为我希望我的“podcast”与我的帖子共享“category”分类法。我有一个归档页面(archive podcasts.php),它在url/podcasts/处显示播客,但它shows all the categories. 我想在中显示我的播客a single category at a time, 比如在mysite。com/podcasts/category/mycategory/

或者更好:mysite。com/category/mycategory/podcasts/

理想情况下,它将使用我现有的存档播客。php模板。

以下是我的自定义帖子类型创建:

function podcast_post_type() {

// Set UI labels for Podcast post Type
    $labels = array(
       //redacted my labels because they aren\'t important here.
    );

// Set other options for Podcast post Type

    $args = array(
        \'label\'               => __( \'podcasts\', \'understrap\' ),
        \'description\'         => __( \'Podcasts\', \'understrap\' ),
        \'labels\'              => $labels,
        // Features this CPT supports in Post Editor
        \'supports\'            => array( \'title\', \'editor\', \'excerpt\', \'author\', \'thumbnail\', \'comments\', \'revisions\', \'custom-fields\', ),
        // You can associate this CPT with a taxonomy or custom taxonomy. 
        \'taxonomies\'          => array(  \'category\' ),
        /* A hierarchical CPT is like Pages and can have
        * Parent and child items. A non-hierarchical CPT
        * is like Posts.
        */  
        \'hierarchical\'        => false,
        \'public\'              => true,
        \'show_ui\'             => true,
        \'show_in_menu\'        => true,
        \'show_in_nav_menus\'   => true,
        \'show_in_admin_bar\'   => true,
        \'menu_position\'       => 5,
        \'can_export\'          => true,
        \'has_archive\'         => true,
        \'exclude_from_search\' => false,
        \'publicly_queryable\'  => true,
        \'capability_type\'     => \'post\',
    );

    // Registering your Podcast post Type
    register_post_type( \'podcasts\', $args );

}
以下代码似乎很有希望,但不起作用:

add_action(\'init\', \'category_cpt_rewrites\');

function category_cpt_rewrites() {
    $custom_post_types = array(\'podcasts\'); //some example post types
    foreach ( $custom_post_types as $post_type ) {
        $rule = \'^\' . $post_type . \'/category/(.+?)/?$\';
        $rewrite = \'index.php?post_type=\' . $post_type . \'&category_name=$matches[1]\';
        add_rewrite_rule($rule,$rewrite,\'top\');
    }
}
实际上,我有点期待这会自动工作——通过在我的帖子类型中添加“类别”分类法,我认为Wordpress会有一个url,我可以在那里访问我的播客类别。

我为此浪费了很多时间,看起来应该很简单——感谢您的帮助!

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

试试这个,让我们知道它是否有效:


function custom_category_rewrite() {

    /**
     * Here you build an array containing the post type keys that you want
     * the rule to work for.
     */
    $custom_post_types = array(\'podcast\');

    foreach ( $custom_post_types as $post_type ) {
        /**
         * Adds rewrite rule which will rewrite sth like http://yoursite.com/podcast/category/category-name/ to
         * http://yoursite.com/index.php?post_type=podcast&category_name=category-name
         */
        add_rewrite_rule( "^$post_type/category/(.+)/?$", \'index.php?post_type=\' . $post_type . \'&category_name=$matches[1]\', \'top\' );
    }

    flush_rewrite_rules();

}

add_action( \'init\', \'custom_category_rewrite\' );

将代码放入函数中。php。

请注意片段中的注释。

尝试访问以下内容http://yoursite.com/podcast/category/category-name/ 执行测试。

相关推荐

如何控制根据Taxonomy术语显示什么模板?

我正在建立一个商业目录,并将给出一些背景,然后在最后有2个问题。The development URL is: http://svcta.lainternet.biz/The website I am rebuilding is: https://www.visitsimivalley.com/当前网站要求每个分类法类型具有唯一的业务概要文件。例如,如果您是一家酒店,并且您也有会议室和婚礼场地,那么您最终会得到3个列表,一个用于酒店,一个用于会议,一个用于婚礼。我希望有一个主配置文件,其中包含我们将显示的