显示帖子类别明智

时间:2014-08-26 09:20:26

标签: wordpress

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

5 个答案:

答案 0 :(得分: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 ?>

答案 1 :(得分:0)

<?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 :(得分:0)

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

答案 3 :(得分:0)

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

答案 4 :(得分:0)

[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();
}