简体   繁体   English

wordpress the_category用于自定义帖子类型

[英]wordpress the_category for custom post types

I have this loop that gets chosen categories for a custom post type of portfolio: 我有这个循环,可以为自定义帖子类型的投资组合选择类别:

$wp_query = new WP_Query(array('category__in' => $portfolio_cat, 'post_type' => 'portfolio', 'showposts'=>$number_of_posts) );

It uses standard wordpress categories, but only shows portfolio type posts in those categories. 它使用标准wordpress类别,但仅显示这些类别中的投资组合类型帖子。 The problem is, I want to list the category the portfolio post is in but when the category is clicked it leads to a 404 not found. 问题是,我想列出投资组合帖子所在的类别,但是当点击该类别时,它会导致找不到404。

I am using this to display the category (successfully) under the portfolio post type: 我使用它来显示投资组合帖子类型下的类别(成功):

<?php the_category('') ?>

Is there any way to have a custom post type use standard categories and not a custom taxonomy and be able to click the category to go to a page that lists all posts in the category? 有没有办法让自定义帖子类型使用标准类别而不是自定义分类,并且能够单击类别转到列出该类别中所有帖子的页面?

You have to use 你必须使用

'taxonomies' => array('category')

in your register_post_type() function to make it connected to the category. register_post_type()函数中使其连接到类别。 Also, you can use this 此外,您可以使用它

register_taxonomy_for_object_type('category','portfolio');

after you call the register_post_type() function. 在调用register_post_type()函数之后。

If you want your custom post type posts to show up on standard archives or include them on your home page mixed up with other post types, use the pre_get_posts action hook. 如果您希望自定义帖子类型的帖子显示在标准存档上,或者将它们包含在与其他帖子类型混合的主页上,请使用pre_get_posts操作挂钩。

add_action( 'pre_get_posts', 'add_my_post_types_to_query' );
function add_my_post_types_to_query( $query ) {
    if ( is_home() && $query->is_main_query() )
    {
        $query->set( 'post_type', array( 'post', 'page', 'portfolio' ) );
    }
    return $query;
}

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

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