0
votes

I have a specific category-slug.php page.

I want to display the first category "banner-ads" with only 1 post per page and then below that, display 3 posts per page from the category "featured."

I don't want to use: **query_posts( 'posts_per_page=4' );**

I've tried the pre_get_posts function but can't seem to get it working.

Right now the number of posts per page that's displaying is the number I assigned in Settings->Reading

Here's my current code:

$args1 = array(
    'category_name' => 'banner-ads',
    'posts_per_page' => 1
    );


$the_query = new WP_Query( $args1 );


while ( $the_query->have_posts() ) :
    $the_query->the_post();
    ar2_render_posts( null, array ( 'type' => 'node' ), true );
endwhile;

wp_reset_postdata();


$args2 = array(
    'category_name' => 'featured',
    'posts_per_page' => 3
    );

$query2 = new WP_Query( $args2 );

while( $query2->have_posts() ):
    $query2->next_post();
    ar2_render_posts( null, array ( 'type' => 'traditional' ), true );
endwhile;

wp_reset_postdata();
3

3 Answers

0
votes

You haven't made reset: wp_reset_postdata();

Useful about categories: http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters

Here is mine for one of pages First loop:

<?php 
$postq1 = new WP_Query(
 array(
  'post_type' => array('post','yourcustom'),
  'posts_per_page' => 1, 
  'category_name'=>'banner-ads')
 );
if($postq1->have_posts()):
    while ( $postq1->have_posts() ) :
        $postq1->the_post();?>
<article id="post-<?php the_ID();?>">....</article>
<?php 
    endwhile;
endif;
wp_reset_postdata();
?>

Second loop:

<?php 
$postq2 = new WP_Query(
 array(
  'post_type' => array('post','yourcustom'),
  'posts_per_page' => 3,
  'category_name'=>'featured')
);
if($postq2->have_posts()):
    while ( $postq2->have_posts() ) :
        $postq2->the_post();?>
<article id="post-<?php the_ID();?>">....</article>
<?php 
    endwhile;
endif;
wp_reset_postdata();
?>

... rest of code with query_posts();

0
votes

Okay so I finally figured it out. If you want to force it to take certain number of posts then add the filter with its function then at the end of the loop remove it. I realized for my second loop that the ar2_render_posts() function was conflicting with the code so I'm opting to redoing that function from scratch since its basically a layout function.

add_filter('post_limits', 'my_post_limits');

function my_post_limits( $limit ) {
    if ( in_category('banner-ads') ) {
        return 'LIMIT 0, 3';
    }
    return $limit;
}

    $args1 = array(
        'category_name' => 'banner-ads'
        );


    $the_query = new WP_Query( $args1 );


    while ( $the_query->have_posts() ) :
        $the_query->the_post();
        ar2_render_posts( null, array ( 'type' => 'node' ), true );
    endwhile;

    wp_reset_postdata();
    remove_filter('post_limits', 'my_post_limits');
0
votes

it is pretty simple, just alter query_args in ar2_render_posts

ar2_render_posts( null, array ( 
     'query_args' => array ( 'posts_per_page' => 1 ),
), true );