【问题标题】:Wordpress get_category or get_postWordpress get_category 或 get_post
【发布时间】:2021-06-16 09:32:07
【问题描述】:

我不知道如何解释我的问题,但我喜欢在指定部分中回显特定类别/ slug(按类别排序)。我不知道应该使用哪个 Wordpress 功能?它是 get_category 还是 get_post? 到目前为止,我有这段代码可以在一个部分中回显所有类别:

         <?php
         $args = array(
            'post_type' => 'allproducts',
            'posts_per_page' => 9,
         );
            $posts = get_posts( $args );
            foreach ( $posts as $post ):
            setup_postdata( $post );
        ?>   

基本上我的目标是获取特定类别的所有帖子: 我有这些具有特定 slug 和 ID 的类别列表:

Category Name | Slug | ID
Shoes         | cat01|  2
Pants         | cat02|  3
Tops          | cat03|  4

【问题讨论】:

    标签: php wordpress


    【解决方案1】:
    $categories = get_categories( array(
      'orderby' => 'name',
    ) );
    
    foreach($categories as $category)
    {
      $cateid = $category->term_id;
      $args = array(
            'post_type' => 'allproducts',
            'category' => $cateid,
            'posts_per_page' => 9,
         );
      $posts = get_posts( $args );
      foreach ( $posts as $post ){
    // here rest of code of posts.
      }      
    }
    

    【讨论】:

      【解决方案2】:

      我想通了。这是我的代码:

              <?php 
                  $args1 = array( 'posts_per_page' => 9, 'offset'=> 0, 'category' => array(1));
                  $myposts1 = get_posts( $args1 );
              
                  foreach ( $myposts1 as $post ) : setup_postdata( $post ); 
              ?>    
      

      array (1) --> 是我的类别的id

      用这段代码关闭它

                      <?php endforeach; 
                      wp_reset_postdata();?>   
      

      【讨论】:

        猜你喜欢
        • 2020-12-29
        • 2015-09-21
        • 1970-01-01
        • 1970-01-01
        • 2012-11-11
        • 1970-01-01
        • 2018-01-30
        • 2018-12-31
        • 1970-01-01
        相关资源
        最近更新 更多