Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

i'm using a query-loop on my template pages like this:

<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$wp_query = new WP_Query();
$wp_query->query('posts_per_page='.get_option('posts_per_page').'&paged=' . $paged);
global $wp_query;
    query_posts(array_merge($wp_query->query, array(
        'paged'          => get_query_var('paged'),
        'posts_per_page' => 2
    )));
?>
<?php if ($wp_query->have_posts()) : ?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
        <div class="portfolioitem">
    <a href="<?php the_permalink() ?>">
                <?php the_post_thumbnail( 'portfolioteaser' ); ?>
                <div class="mask">
                    <h2><?php the_title(); ?></h2>
                </div>
            </a>
        </div>
         <?php endwhile; ?>
<?php endif; ?> 

the pagination works fine with this except on the category.php or tag.php. The first page is showing correctly but for example if i click on "category/categoryname/page/2" it shows no content anymore an dthe pagination disappears...

i know it must be something wrong with the query loop...

help would be nice! thanks alot, Jochen

share|improve this question
1  
Why are you calling query_posts()? That's your problem. The fix is to get rid of query_posts(). –  Chip Bennett Feb 19 '13 at 15:00
    
could you give me an example how to change my loop? –  Jochen Schmidt Feb 19 '13 at 15:07
    
You don't need to change your loop. You just need to filter the $query properly. If you need/want to do more than just change posts_per_page, please update your question to indicate your needed changes. –  Chip Bennett Feb 19 '13 at 16:24
    
Also, for future reference, please be sure to search for related posts before asking new questions. Questions about issues with pagination for custom loops are fairly common here, and is probably the reason for the downvote on your question. –  Chip Bennett Feb 19 '13 at 16:35
add comment

1 Answer 1

up vote 0 down vote accepted

If you merely want to modify posts_per_page for tag and category archive index pages, don't use query_posts(); instead, filter the $query via pre_get_posts:

function wpse87489_filter_pre_get_posts( $query ) {
    if ( ( is_category() || is_tag() ) && $query->is_main_query ) {
        $query->set( 'posts_per_page', '2' );
    }
}
add_action( 'pre_get_posts', 'wpse87489_filter_pre_get_posts' );
share|improve this answer
    
thanks for your quick answer... do i have to add this to my functions.php? and how do i set up my loop? could you give me an example how to change it? thanks alot... –  Jochen Schmidt Feb 19 '13 at 15:08
    
Add filters/hooks can callbacks to functions.php. Use the normal loop, with no calls to query_posts() or new WP_Query(). Just call if ( have_posts() ) : while ( have_posts() ) : the_post(); –  Chip Bennett Feb 19 '13 at 16:23
    
it works.. thanks man! –  Jochen Schmidt Feb 19 '13 at 21:02
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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