【问题标题】:Get all posts from different categories created in custom post type获取以自定义帖子类型创建的不同类别的所有帖子
【发布时间】:2021-12-28 13:30:42
【问题描述】:

我用这段代码在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');

现在我正在尝试在我的前端获取/显示我在此处创建的具有不同类别的所有帖子

<?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; 
?>

但我无法让它工作。现在我得到了所有不同的类别,这很好,但是具有相同类别的帖子应该直接打印在后面而不是上面的相同类别名称。

【问题讨论】:

    标签: php wordpress post


    【解决方案1】:

    您需要使用 "get_cat_name( $category_id )" 而不是 "the_category()"

    所以在函数 get_cat_name() 中,您需要传递您在 wp_query 中传递的相同类别 ID。

    【讨论】:

      【解决方案2】:

      这是您的代码,如果您希望显示所有帖子,那么您不需要传递任何类别,您只需传递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();
      

      如果您希望显示相应类别的特定帖子,则可以使用 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();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-08-05
        • 2017-10-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-11
        • 1970-01-01
        • 2021-08-09
        相关资源
        最近更新 更多