简体   繁体   English

获取以自定义帖子类型创建的不同类别的所有帖子

[英]Get all posts from different categories created in custom post type

I have created a custom post type in functions.php with this code.我使用此代码在 functions.php 中创建了一个自定义帖子类型。

function create_recipes() {
        register_post_type('recipe', [
            'public' => true,
            'show_in_rest' => true,
            'labels' => [
                'name' => 'Recipes',
                'add_new_item' => 'Add New Recipe',
                'edit_item' => 'Edit Recipe',
                'all_items' => 'All Recipes',
                'singular_name' => 'Recipe',
            ],
            'supports' => ['title', 'editor'],
            'rewrite' => ['slug' => 'recipes'],
            'menu_icon' => 'dashicons-media-archive',
            'has_archive' => true,
            
            'taxonomies'  => array('category'),
            'supports' => array(  'title', 'editor', 'author', 'thumbnail' ),   
        ]);
    }
    
    add_action('init', 'create_recipes');

And now I am trying to get/show all the posts on my frontend that I've created that have different categories here现在我正在尝试获取/显示我在前端创建的所有帖子,这些帖子在此处具有不同的类别

<?php

                        $recipes = new WP_Query([
                            'post_type' => 'recipe',
                            'category' => '01'
                        ]);
                        
                        while($recipes->have_posts()):
                            $recipes->the_post(); 
                            ?>
                            <div class="sub-column">
                                <div class="sub-cat">
                                    <?php the_category(); ?>
                                </div>
                                <a>
                                <div class="sub-thumbnail">
                                        <?php echo the_post_thumbnail(); ?>
                                    </div>
                                </a>
                                <div class="sub-title">
                                    <h4><?php the_title(); ?></h4>
                                </div>
                            </div>
                            <?php endwhile; ?>

But I cant get it to work.但我无法让它工作。 Now I get all the different categories which is good but the posts that have the same category should be printent directly after and not with the same category name above.现在我得到了所有不同的类别,这很好,但是具有相同类别的帖子应该直接打印出来,而不是上面的相同类别名称。

Help appriciated!帮助赞赏!

You need to use the "get_cat_name( $category_id )" rather then "the_category()"您需要使用“get_cat_name($category_id)”而不是“the_category()”

So in the function get_cat_name(), you need to pass the same category id which you are passing in the wp_query.因此,在 function get_cat_name() 中,您需要传递在 wp_query 中传递的相同类别 ID。

Here is your code and if you are wishing to show all the posts then you do not require to pass any category all you need to pass is the post_per_page这是您的代码,如果您希望显示所有帖子,那么您不需要传递任何类别,您需要传递的只是post_per_page

// WP_Query arguments
$args = array(
    'post_type'              => array( 'recipes' ),
    'post_status'            => array( 'publish' ),
    'posts_per_page'         => '-1',
    'order'                  => 'DESC',
    'orderby'                => 'id',
);

// The Query
$query = new WP_Query( $args );

// The Loop
if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();
        // do something
        echo get_the_title();
    }
} else {
    // no posts found
}

// Restore original Post Data
wp_reset_postdata();

If you wish to show the specific post of the respective category then you can use slug, term_id, etc and you need to inject the tax_query in your argument here is the sample code based on slug如果您希望显示相应类别的特定帖子,则可以使用 slug、term_id 等,并且您需要在参数中注入 tax_query 这里是基于 slug 的示例代码

// WP_Query arguments
$args = array(
    'post_type'              => array( 'recipes' ),
    'post_status'            => array( 'publish' ),
    'posts_per_page'         => '-1',
    'tax_query' => array(
                        array(
                            'taxonomy' => 'category', // taxonomy slug
                            'field' => 'slug', //do not change this if you wisht to fetch from slug
                            'terms' => 'bob' //slug name
                        )
                    )
    'order'                  => 'DESC',
    'orderby'                => 'id',

);

// The Query
$query = new WP_Query( $args );

// The Loop
if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();
        // do something
        echo get_the_title();
    }
} else {
    // no posts found
}

// Restore original Post Data
wp_reset_postdata();

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

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