简体   繁体   English

将自定义帖子类型分类法显示为存档页面

[英]Display custom post type taxonomies as an archive page

Hello I have a custom post type called results . 您好,我有一个名为results的自定义帖子类型。 I also created categories for that specific post type using a taxonomy. 我还使用分类法为该特定帖子类型创建了类别。 I'm not sure if I set it up correctly, but the code I have works so I'm sticking with it. 我不确定是否设置正确,但是我拥有的代码有效,因此我坚持使用。 If you see a better way or any errors please let me know. 如果您发现更好的方法或任何错误,请告诉我。

I am able to create a custom post and set a category to it. 我能够创建自定义帖子并为其设置类别。 Next I would like to create a categories page that will act like the regular archive.php but just for the category of the custom post types. 接下来,我想创建一个类别页面,该页面的行为类似于常规的archive.php,但仅用于自定义帖子类型的类别。

So say I have a custom post for results and I have its category set to car accidents I would like a way to display them all just like archive.php does for normal posts. 所以说我有一个自定义results帖子,并且我将其类别设置为car accidents我想像普通存档一样显示所有这些内容,就像archive.php一样。

I tried going to a url like this but I get sent to a 404 page, even though I have an archive-results.php 我试着转到这样的网址,但即使有archive-results.php,也会被发送到404页面

www.myurl.com/results/categories/car-accidents

Here is the code I used to set up the custom post type and the taxonomy. 这是我用来设置自定义帖子类型和分类法的代码。 Sorry If its long but I feel like its necessary to include everything. 抱歉,如果它很长,但是我觉得有必要包括所有内容。

// Create custom post type
function create_posttype() {
    register_post_type( 'Results',
        array(
            'labels' => array(
                'name' => __( 'Results' ),
                'singular_name' => __( 'Results' )
            ),
            'public' => true,
            'has_archive' => true,
            'rewrite' => array('slug' => 'results'),
            'taxonomies'  => array( 'results', 'result-category' ),
        )
    );
}
add_action( 'init', 'create_posttype' );

//Create category for specific post type
function tr_create_my_taxonomy() {
    register_taxonomy(
        'results-categories',
        'results',
        array(
            'label' => __( 'Result Categories' ),
            'rewrite' => array( 'slug' => 'result-category' ),
            'hierarchical' => true,
            'has_archive' => true
        )
    );
}
add_action( 'init', 'tr_create_my_taxonomy' );

Am I missing something that is preventing this url from working? 我是否缺少阻止此网址正常工作的内容?

www.myurl.com/results/categories/car-accidents

Thanks in advance 提前致谢

function namespace_add_custom_types( $query ) {
if( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {
$query->set( 'post_type', array(
 'post', 'nav_menu_item', 'cmc-description'
    ));
  return $query;
}
}
add_filter( 'pre_get_posts', 'namespace_add_custom_types' );
 // Create custom post type
function create_posttype() {
    register_post_type( 'Results',
array(
        'labels' => array(
            'name' => __( 'Results' ),
            'singular_name' => __( 'Results' )
        ),
        'public' => true,
        'has_archive' => true,
        'rewrite' => array('slug' => 'results'),
        'taxonomies'  => array( 'results', 'result-category' ),
    )
);
flush_rewrite_rules();
}
add_action( 'init', 'create_posttype' );

//Create category for specific post type
function tr_create_my_taxonomy() {
register_taxonomy(
    'results-categories',
    'results',
    array(
        'label' => __( 'Result Categories' ),
        'rewrite' => array( 'slug' => 'result-category' ),
        'hierarchical' => true,
        'has_archive' => true
    )
);
}
add_action( 'init', 'tr_create_my_taxonomy' );

i Just made this changes can you please copy this to your code and see it runs well or not 我刚刚进行了此更改,您能否将其复制到您的代码中,看看它是否运行良好

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM