【问题标题】:How to retrieve posts(books) based on category(taxonomy) in wordpress?如何在wordpress中根据类别(分类)检索帖子(书籍)?
【发布时间】:2019-12-26 12:51:43
【问题描述】:

我正在尝试显示特定类别(分类)的帖子,即“Book1”。

我尝试使用以下代码显示它。

                    $args = array(
                        'post_type' => 'book',
                        'posts_per_page' => 6,
                        'tax_query' => array(
                            array(
                                'taxonomy' => 'Book1',
                                'field' => 'id',
                                'terms' => 1
                            )
                        )
                    );
                    echo '<br>';
                    $postss = get_posts( $args );

                    if ( ! empty( $postss ) && is_array( $postss ) ) {
                        // Run a loop and print them all
                        $i=1;
                        foreach ( $postss as $termm ) { 
                                echo ' '.$i.' '.$termm->post_title. '<br>';
                                $i++;
                        }
                    }
               ?>

在输出中没有显示任何项目。

【问题讨论】:

标签: wordpress wordpress-theming


【解决方案1】:
$custom_terms = get_terms('Book1');

foreach($custom_terms as $custom_term) {
    wp_reset_query();
    $args = array(
        'post_type' => 'book',
        'posts_per_page' => 6,
        'tax_query' => array(
            array(
                'taxonomy' => 'Book1',
                'field' => 'slug',
                'terms' => $custom_term->slug,
            ),
        ),
     );

    $loop = new WP_Query($args);
    if($loop->have_posts()) {
        echo '<h2>'.$custom_term->name.'</h2>';

        while($loop->have_posts()) : $loop->the_post();
            echo '<a href="'.get_permalink().'">'.get_the_title().'</a><br>';
        endwhile;
    }
}

试试这个代码

【讨论】:

    【解决方案2】:
    $args = array(
            'post_type' => 'book',
            'posts_per_page' => 6,
            'tax_query' => array(
                array(
                    'taxonomy' => 'Book1',
                    'field' => ''term_id', // here you are worng name too
                    'terms' => 1
                )
            )
        );
        echo '<br>';
        $postss = get_posts( $args );
    
        if ( ! empty( $postss ) && is_array( $postss ) ) {
            // Run a loop and print them all
            $i=1;
            foreach ( $postss as $termm ) { 
                    echo ' '.$i.' '.$termm->post_title. '<br>';
                    $i++;
            }
        }
    

    ?>

    // 最佳解决方案

    <?php
    
    $query = new WP_Query( array(
        'post_type' => 'book',          // name of post type.
        'tax_query' => array(
            array(
                'taxonomy' => 'Book1',   // taxonomy name
                'field' => 'term_id',           // term_id, slug or name
                'terms' => 1,                  // term id, term slug or term name
            )
        )
    ) );
    
    while ( $query->have_posts() ) : $query->the_post();
        // do stuff here....
    endwhile;
    
    /**
     * reset the orignal query
     * we should use this to reset wp_query
     */
    wp_reset_query();
    ?>
    

    【讨论】:

      【解决方案3】:

      我是这样做的。谢谢大家。

              $args = array(
                  'post_type' => 'book',
                  'cat' => '35'
              );
              echo '<br>';
              $postss = query_posts($args);
              if ( ! empty( $postss ) && is_array( $postss ) ) {
                  // Run a loop and print them all
                  ?><?php $i=1;
                  foreach ( $postss as $termm ) { ?>
      
                          <?php echo ' '.$i.' '.$termm->post_title. '<br>';$i++;?>
                      <?php
                  }
              }
      

      【讨论】:

        猜你喜欢
        • 2012-01-21
        • 1970-01-01
        • 1970-01-01
        • 2021-04-30
        • 2012-09-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多