【问题标题】:Display Posts Category Wise明智地显示帖子类别
【发布时间】:2020-08-28 16:04:56
【问题描述】:

我有代码显示每个类别的最新 3 个帖子,我想排除子类别,但它不排除子类别,它单独显示子类别,它显示子类别的最新 3 个帖子。有没有办法从循环中排除子类别。

代码如下:

<?php
//start page loop
if (have_posts()) : while (have_posts()) : the_post();

    //get meta to set parent category
    $library_filter_parent = '';
    $library_parent = get_post_meta($post->ID, 'wts_library_parent', true);
    if($library_parent != 'select_library_parent') { $library_filter_parent = $library_parent; } else { $library_filter_parent = NULL; }
    ?>

    <div id="library-by-category-wrap">

        <?php
        //get meta to set parent category
        $library_filter_parent = '';
        $library_parent = get_post_meta($post->ID, 'wts_library_parent', true);
        if($library_parent != 'select_library_parent') { $library_filter_parent = $library_parent; } else { $library_filter_parent = NULL; };

        //term loop
        $terms = get_terms('library_cats','orderby=custom_sort&hide_empty=1&child_of='.$library_filter_parent.'');
        foreach($terms as $term) { ?>

            <div class="heading">
                <h2><?php echo $term->name; ?></h2>
            </div>

            <div class="library-category">

                <?php
                //tax query
                $tax_query = array(
                    array(
                        'taxonomy' => 'library_cats',
                        'terms' => $term->slug,
                        'field' => 'slug'
                    )
                );
                $term_post_args = array(
                    'post_type' => 'library',
                    'numberposts' => '3',
                    'tax_query' => $tax_query
                );
                $term_posts = get_posts($term_post_args);

                //start loop
                foreach ($term_posts as $post) : setup_postdata($post);

                    //get images
                    $featured_image = get_the_post_thumbnail($post->ID, 'cat-thumbnail'); ?>
                      <?php if(!empty($featured_image)) { ?>
                          <div class="library-item">
                            <a class="library-title" href="#" title="<?php the_title(); ?>" target="_blank">
                                <h3><?php the_title(); ?></h3>
                            </a>
                          </div>
                          <!-- /library-item -->
                      <?php } ?>

                <?php endforeach; ?>
            </div>
            <!-- /library-category -->

        <?php } wp_reset_postdata(); ?>

    </div>
    <!-- /library-by-category-wrap -->

    <?php wp_reset_query(); ?>

<?php endwhile; endif; ?>

【问题讨论】:

    标签: wordpress


    【解决方案1】:
    <?php
    //get all terms (e.g. categories or post tags), then display all posts in each term
    $taxonomy = 'category';//  e.g. post_tag, category
    $param_type = 'category__in'; //  e.g. tag__in, category__in
    $term_args=array(
      'orderby' => 'name',
      'order' => 'ASC'
    );
    $terms = get_terms($taxonomy,$term_args);
    if ($terms) {
      foreach( $terms as $term ) {
        $args=array(
          "$param_type" => array($term->term_id),
          'post_type' => 'post',
          'post_status' => 'publish',
          'posts_per_page' => -1,
          'caller_get_posts'=> 1
          );
        $my_query = null;
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {
          echo 'List of Posts in '.$taxonomy .' '.$term->name;
          while ($my_query->have_posts()) : $my_query->the_post(); ?>
            <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
           <?php
          endwhile;
        }
      }
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>
    

    注意请参阅 query_posts() 文章中的时间参数。

    【讨论】:

      【解决方案2】:
      <?php // get all the categories from the database
      $cats = get_categories();
      // loop through the categries
      foreach ($cats as $cat) {
      // setup the cateogory ID
      $cat_id= $cat->term_id;
      // Make a header for the cateogry
      echo "<h2>".$cat->name."</h2>";
      // create a custom wordpress query
      query_posts(“cat=$cat_id&post_per_page=100");
      if (have_posts()) : while (have_posts()) : the_post(); ?>
      <?php // create our link now that the post is setup ?>
      <a href="<?php the_permalink();?>"><?php the_title(); ?></a>
      <?php echo '<hr/>'; ?>
      
      <?php endwhile; endif;
      // done our wordpress loop. Will start again for each category ?>
      
      <?php } // done the foreach statement ?>
      

      【讨论】:

        【解决方案3】:
        <?php
            $catquery = new WP_Query( array( 'post_type' => 'testimonials', 'category_name'=>'hello','posts_per_page' => 10 ) );
            while($catquery->have_posts()) : $catquery->the_post();
        ?>
        <ul>
            <li>
                <h3>
                    <a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a>
                </h3>
            </li>
            <li>
                <?php the_content(); ?>
            </li>
        </ul>
        <?php endwhile; ?>
        

        【讨论】:

          【解决方案4】:
          $taxonomy = 'category';
          $term_args = array(
            'orderby' => 'name',
            'order' => 'ASC',
            'parent' => 0
          );
          $terms = get_terms($taxonomy, $term_args);
          

          【讨论】:

          • 嗨@Navnit,这里通常不赞成仅代码的答案。并且有充分的理由(见皮埃尔的评论)。您的答案应始终包含对您的代码完成的内容以及 OP 应该修改的原因和内容的一些描述。请相应地编辑您的答案,谢谢!
          【解决方案5】:
          [homepage_cat_grid limit='3' cat_id='2']
          
          add_shortcode( 'homepage_cat_grid', array($this, 'homepage_postgrid_shortcode') );
          
          /**
           *  HOME PAGE CATEGORY WISE POST GRID
           */
          public function homepage_postgrid_shortcode( $atts, $content ) {
              
              extract( shortcode_atts( array (
                  'order'    => '',
                  'orderby'  => '',
                  'limit' => '',
                  'cat_id'=> ''
              ), $atts ) );
              ob_start();
              $args = array(
                  'post_type' => 'post',
                  'order'     => $order,
                  'orderby'   => $orderby,
                  'posts_per_page' => $limit,
                  'cat'         => $cat_id
              );
              $home_post = new WP_Query( $args );
              
              if( $home_post->have_posts() ) : ?> 
              
              <div class="post-header">
                  <div class="head-left">
                      <?Php 
                          $terms = wp_get_post_terms($home_post->post->ID,'category'); 
                          $cat_nm=$terms[0]->name; 
                          if($cat_nm=='News'): echo "Recent News"; else: echo $cat_nm; endif;  
                      ?>
                  </div>  
                  <a href="<?php
                  $category_link = get_category_link( $terms[0]->term_id );
                  echo $category_link; ?>" class="btn post-head-btn">VIEW ALL</a>
              </div>  
              
              <div class="row">
                  <?php while ( $home_post->have_posts() ) : $home_post->the_post(); ?>
                      <div class="col-md-4">
                          <div class="single-grid">
                              <a href="<?php echo get_permalink( $post->ID ); ?>">
                                  <?php the_post_thumbnail( $post->ID ); ?>
                              </a>
                              <div class="post-caption">
                                  <?php echo the_category(); ?>
                                  <div class="post-heading">
                                      <a href="<?php echo get_permalink( $post->ID ); ?>"><?php echo the_title(); ?></a>
                                  </div>
                                  <div class="post-content">
                                      <?php the_excerpt(); ?>
                                  </div>
                                  <div class="row">
                                      <div class="col-12">
                                          <a class="btn btn-dark cust-btn" href="<?php echo get_permalink( $post->ID ); ?>">Read More</a>
                                      </div>
                                  </div>
                              </div>
                          </div>
                      </div>
                  <?php endwhile; wp_reset_query(); ?>
              </div>  
              
              <?php else :
                      get_template_part( 'template-parts/content', 'none' );
                  endif; 
                  return ob_get_clean();
          }
          

          【讨论】:

          • 这是您在没有太多上下文的情况下提供的大量代码。也许它确实提供了问题的答案,但您至少可以解释一下这段代码的作用和工作原理吗?甚至代码中的一些 cmets 也会有所帮助。感谢您的贡献。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多