显示文章类别智者(Display Posts Category Wise)

2019-10-20 19:30发布

我有代码来显示每个类别的最新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; ?>

Answer 1:

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


Answer 2:

<?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()的文章。



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


Answer 4:

$taxonomy = 'category';
$term_args = array(
  'orderby' => 'name',
  'order' => 'ASC',
  'parent' => 0
);
$terms = get_terms($taxonomy, $term_args);


文章来源: Display Posts Category Wise
标签: wordpress