尝试让自定义帖子类型和帖子显示在标签和类别页面中

发布于 2024-11-09 14:50:25 字数 2895 浏览 0 评论 0原文

我构建了一个自定义帖子类型,它设置为使用帖子使用的开箱即用标签和类别。但是,如果我单击标签或类别链接,存档将仅显示带有该标签的帖子,而不是我的自定义帖子类型。我尝试了几种方法来修复它,但它们似乎不起作用。我的代码是:

// Add Resource Post Type

add_action('init', 'hallam_init');

function hallam_init() {
        // set up the labels
        $labels = array(
                'name' => _x('Resources', 'post type general name'),
                'singular_name' => _x('Resource', 'post type singular name'),
                'add_new' => _x('Add New', 'resource'),
                'add_new_item' => __( 'Add New Resource' ),
                'edit_item' => __( 'Edit Resource' ),
                'new_item' => __( 'New Resource' ),
                'view_item' => __( 'View Resource' ),
                'search_items' => __( 'Search Resources' ),
                'not_found' =>  __( 'No resources found' ),
                'not_found_in_trash' => __( 'No respources found in Trash' ),
                'parent_item_colon' => ''
        );

        // set up the args
        $args = array (
                'labels' => $labels,
                'public' => true,
                'publicly_queryable' => true,
                'show_ui' => true,
                'query_var' => true,
                'rewrite' => array (
                        'slug' => 'resources'
                ),
                'capability_type' => 'post',
                'hierarchical' => false,
                'menu_position' => 5,
                'supports' => array(
                        'title',
                        'editor',
                        'author',
                        'thumbnail',
                        'excerpt',
                        'comments'
                ),
                'taxonomies' => array(
                        'collection',
                        'category',
                        'post_tag'
                ),
                'has_archive' => true
        );

        register_post_type('ht_resource', $args);
}

// Add Taxonomy

register_taxonomy('collection', 'ht_resource', array(
        'hierarchical' => true,
        'label' => 'Collections',
        'query_var' => true,
        'rewrite' => true
));

// Fix the archives

    add_filter( 'pre_get_posts', 'add_to_query' );

    function add_to_query( $query ) {
        // if ( is_home() ) {
            if( $query->query_vars['suppress_filters'] ) // TODO check if necessary
                return $query;
            $supported = $query->get( 'post_type' );
            if ( !$supported || $supported == 'post' )
                $supported = array( 'post', 'ht_resource' );
            elseif ( is_array( $supported ) )
                array_push( $supported, 'ht_resource' );
            $query->set( 'post_type', $supported );
            return $query;
        //}
    }

我错过了一些明显的东西吗?

I've built a custom post type, that is set to use the out-of-the-box tags and categories that posts use. However, if I click on a tag or category link, the archive only shows posts with that tag, not my custom post type. I've tried a few ways to fix it but they don't seem to be working. My code is:

// Add Resource Post Type

add_action('init', 'hallam_init');

function hallam_init() {
        // set up the labels
        $labels = array(
                'name' => _x('Resources', 'post type general name'),
                'singular_name' => _x('Resource', 'post type singular name'),
                'add_new' => _x('Add New', 'resource'),
                'add_new_item' => __( 'Add New Resource' ),
                'edit_item' => __( 'Edit Resource' ),
                'new_item' => __( 'New Resource' ),
                'view_item' => __( 'View Resource' ),
                'search_items' => __( 'Search Resources' ),
                'not_found' =>  __( 'No resources found' ),
                'not_found_in_trash' => __( 'No respources found in Trash' ),
                'parent_item_colon' => ''
        );

        // set up the args
        $args = array (
                'labels' => $labels,
                'public' => true,
                'publicly_queryable' => true,
                'show_ui' => true,
                'query_var' => true,
                'rewrite' => array (
                        'slug' => 'resources'
                ),
                'capability_type' => 'post',
                'hierarchical' => false,
                'menu_position' => 5,
                'supports' => array(
                        'title',
                        'editor',
                        'author',
                        'thumbnail',
                        'excerpt',
                        'comments'
                ),
                'taxonomies' => array(
                        'collection',
                        'category',
                        'post_tag'
                ),
                'has_archive' => true
        );

        register_post_type('ht_resource', $args);
}

// Add Taxonomy

register_taxonomy('collection', 'ht_resource', array(
        'hierarchical' => true,
        'label' => 'Collections',
        'query_var' => true,
        'rewrite' => true
));

// Fix the archives

    add_filter( 'pre_get_posts', 'add_to_query' );

    function add_to_query( $query ) {
        // if ( is_home() ) {
            if( $query->query_vars['suppress_filters'] ) // TODO check if necessary
                return $query;
            $supported = $query->get( 'post_type' );
            if ( !$supported || $supported == 'post' )
                $supported = array( 'post', 'ht_resource' );
            elseif ( is_array( $supported ) )
                array_push( $supported, 'ht_resource' );
            $query->set( 'post_type', $supported );
            return $query;
        //}
    }

Am I missing something obvious?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我是有多爱你 2024-11-16 14:50:25

您可以通过添加主题的functions.php来尝试一下吗?希望它能发挥作用

function query_post_type($query) {
  if(is_tag()) {
        $query->set('post_type',$post_types=get_post_types('','names'));
        return $query;
    }
}
add_filter('pre_get_posts', 'query_post_type'); 

,谢谢

Can you plz try this by adding on your theme's functions.php? Hope it will work

function query_post_type($query) {
  if(is_tag()) {
        $query->set('post_type',$post_types=get_post_types('','names'));
        return $query;
    }
}
add_filter('pre_get_posts', 'query_post_type'); 

thnx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文