0

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

4
  • 1
    Why are you calling query_posts()? That's your problem. The fix is to get rid of query_posts(). Commented Feb 19, 2013 at 15:00
  • could you give me an example how to change my loop? Commented Feb 19, 2013 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. Commented Feb 19, 2013 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. Commented Feb 19, 2013 at 16:35

2 Answers 2

2

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' );
2
  • 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... Commented Feb 19, 2013 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(); Commented Feb 19, 2013 at 16:23
0

Please use the following code:

$arg = array (
    'posts_per_page' => get_option('posts_per_page'), 
    'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1 ) 
);
$wp_query = new WP_Query($arg);
if ($wp_query->have_posts()) :
    while ($wp_query->have_posts()) : $wp_query->the_post();
        // do something
    endwhile;
endif;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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