0

I've been trying to display posts that I've categorized as "news-categories". My goal is to only display 4 posts on sidebar of "cbs-news" slug underneath "news-categories" category. By below mention code all the post underneath "cbs-news" are displaying. But i need to display only 4 latest post.

 <ul class="list4 martop5">
    <?php 
            $args = array('tax_query' => array( array('taxonomy' => 'news-categories', 'posts_per_page' => '5', 'field' => 'slug','terms' => 'cbs-news',
            'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1 )
            )));
            $the_query = new WP_Query( $args );
            if($the_query->have_posts() ) : 
            //have_posts("showposts=4");
            while ( $the_query->have_posts() ) : $the_query->the_post();
            ?>
        <li>
           <a href="<?php echo get_permalink(); ?>"><?php the_title(); ?></a>
        </li><!-- /.service -->

    <?php endwhile; else: ?>

        <p>Nothing Here.</p>

    <?php endif; wp_reset_postdata(); ?>
1

You write the wrong code

$paged = get_query_var('paged') ? get_query_var('paged') : 1;
$args = array('posts_per_page' => '5', 
              'paged' => $paged,
              'tax_query' => array( array('taxonomy' => 'news-categories',
                                          'field' => 'slug',
                                          'terms' => 'cbs-news')
                                  )
              );

You can also take help in WP_Query

  • Wow, Its working...Thanks for the help..:) Waise kithe de ho bhaji..:) – Gaurav Jul 23 '13 at 9:43
  • New Delhi de :D – ksr89 Jul 25 '13 at 4:19
0

Your query is not correct, try this instead :

 $paged = ( get_query_var('paged') ? get_query_var('paged') : 1 );

 $args = array(
 'tax_query' => array( 
                  array('taxonomy' => 'news-categories',
                        'field' => 'slug',
                        'terms' => 'cbs-news'
                      )
               ), 
 'posts_per_page' => '5',
 'paged' => $paged
 );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.