WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] wp_query and pagination (8 posts)

  1. Moofin
    Member
    Posted 1 year ago #

    I've been researching this for the past couple of hours and I just can't seem to figure it out.

    So I have an archive page which has the first category post as a 'feature' and then the rest of the category posts are listed below it. This is all working as it should. However, because I'm using query_posts the pagination isn't working as it should.

    Does anyone here know how I could get it to work? Here's my code:

    <?php if (have_posts()) : ?>
    			 <?php query_posts(array("cat" => get_query_var('cat'), 'showposts' => 1)); ?>
                  <?php while (have_posts()) : the_post(); ?>
                  	<div class="arcfeaturepost">
                     <h2><a class="highlight" href="<?php the_permalink();?>"><?php the_title(); ?></a></h2>
    				   <?php if ( has_post_thumbnail() ) { ?><div class="arcfeature"><a href="<?php the_permalink();?>"><?php the_post_thumbnail(); ?></a></div><?php } ?>
    	                <?php  the_excerpt('65');?>
                        <p><a class="readmorea" href="<?php the_permalink();?>">READ MORE ></a></p>
                    </div>
                  <?php endwhile; ?><!-- end FIRST CATEGORY POST FEATURE -->
                 <?php
    			 query_posts(array("cat" => get_query_var('cat'), 'offset' => 1)); ?>
                  <?php while (have_posts()) : the_post(); ?>
                  	<div class="arcpost">
                    	<div class="arcimg"><a href="<?php the_permalink();?>"><?php the_post_thumbnail(); ?></a></div>
    	               	<div class="arctxt">
                            <h3><?php the_title(); ?></h3>
                            <?php  the_excerpt_dynamic(200,'no')?>
                            <p><a class="readmorea" href="<?php the_permalink();?>">READ MORE ></a></p>
                        </div>
                    </div>
                 <?php endwhile; ?>
               	<div class="navigation">
                    <div class="alignleft"><?php previous_posts_link('« Previous') ?></div>
                    <div class="alignright"><?php next_posts_link('More »') ?></div>
                </div>
                <?php else: ?>
                 <!-- Error message when no post published -->
                <?php endif; ?>

    Many thanks

  2. Bob Wagstaff
    Member
    Posted 1 year ago #

    Is this the same as my issue on this thread?

    http://wordpress.org/support/topic/excluding-sticky-posts?replies=2

  3. Shaun Scovil
    Member
    Posted 1 year ago #

    Short answer: don't use query_posts().

    From the Codex:

    It should be noted that using this to replace the main query on a page can increase page loading times, in worst case scenarios more than doubling the amount of work needed or more. While easy to use, the function is also prone to confusion and problems later on. See the note further below on caveats for details.

    For general post queries, use WP_Query or get_posts.

  4. Moofin
    Member
    Posted 1 year ago #

    Okay, so I had a look at the get_posts tag and have implemented it so it's working/displaying how I'd like it. However, I still can't get the pagination to work. I don't know if it's because of how I have it setup.

    It's a magazine type blog so what I'm trying to achieve is:

    ==FEATURED POST WITH BIGGER IMAGE AND MORE EXCERPT & different styling ==

    then directly underneath a small list which will have a thumbnail, small excerpt etc.

    This is what I have now:

    <?php
        global $post;
        $args = array( "cat" => get_query_var('cat'),'showposts' => 1 );
        $myposts = get_posts( $args );
        foreach( $myposts as $post ) : setup_postdata($post);
        ?>
        <div class="arcfeaturepost">
         <h2><a class="highlight" href="<?php the_permalink();?>"><?php the_title(); ?></a></h2>
           <?php if ( has_post_thumbnail() ) { ?><div class="arcfeature"><a href="<?php the_permalink();?>"><?php the_post_thumbnail(); ?></a></div><?php } ?>
            <?php  the_excerpt('65');?>
            <p><a class="readmorea" href="<?php the_permalink();?>">READ MORE ></a></p>
        </div>
      <?php endforeach; ?><!-- end FIRST CATEGORY POST FEATURE -->
     <?php
        global $post;
        $args = array( "cat" => get_query_var('cat'),'offset' => 1 );
        $myposts = get_posts( $args );
        foreach( $myposts as $post ) : setup_postdata($post);
      ?>
        <div class="arcpost">
            <div class="arcimg"><a href="<?php the_permalink();?>"><?php the_post_thumbnail(); ?></a></div>
            <div class="arctxt">
                <h3><?php the_title(); ?></h3>
                <?php  the_excerpt_dynamic(200,'no')?>
                <p><a class="readmorea" href="<?php the_permalink();?>">READ MORE ></a></p>
            </div>
        </div>
     <?php endforeach; ?>
    <div class="navigation">
        <div class="alignleft"><?php previous_posts_link('« Previous') ?></div>
        <div class="alignright"><?php next_posts_link('More »') ?></div>
    </div>

    Have I done it incorrectly?

  5. Shaun Scovil
    Member
    Posted 1 year ago #

    I prefer using WP_Query. Try this:

    EDIT: User posts_per_page with WP_Query!

    <?php
        $args = array(
            'post__in'       => array(),
            'category'       => get_query_var('cat'),
            'posts_per_page' => 1
        );
        $featured = new WP_Query( $args );
    ?>
    
    <!-- Featured Post -->
    
    <?php while( $featured->have_posts() ) : $featured->the_post(); ?>
    
    <div class="arcfeaturepost">
        <h2><a class="highlight" href="<?php the_permalink();?>"><?php the_title(); ?></a></h2>
        <?php if ( has_post_thumbnail() ) { ?><div class="arcfeature"><a href="<?php the_permalink();?>"><?php the_post_thumbnail(); ?></a></div><?php } ?>
        <?php  the_excerpt('65');?>
        <p><a class="readmorea" href="<?php the_permalink();?>">READ MORE ></a></p>
    </div>
    
    <?php endwhile; wp_reset_postdata(); ?>
    
    <!-- Post Archive Loop -->
    
    <?php while( have_posts() ) : the_post(); ?>
    
        <div class="arcpost">
            <div class="arcimg"><a href="<?php the_permalink();?>"><?php the_post_thumbnail(); ?></a></div>
            <div class="arctxt">
                <h3><?php the_title(); ?></h3>
                <?php  the_excerpt_dynamic(200,'no')?>
                <p><a class="readmorea" href="<?php the_permalink();?>">READ MORE ></a></p>
            </div>
        </div>
    
    <?php endwhile; ?>
    
    <!-- Page Navigation -->
    
    <div class="navigation">
        <div class="alignleft"><?php previous_posts_link('« Previous') ?></div>
        <div class="alignright"><?php next_posts_link('More »') ?></div>
    </div>

    By setting the post__in argument to an empty array, WP_Query should only pull in sticky posts...and the other arguments will further filter them by category and quantity.

    From the Codex:

    post__in (array) - use post ids. Specify posts to retrieve. ATTENTION If you use sticky posts, they will be included (prepended!) in the posts you retrieve whether you want it or not. To suppress this behaviour use ignore_sticky_posts

    Then in the post archive loop, you should not need to do another query because the main query in your archive pages will have the correct results.

  6. Moofin
    Member
    Posted 1 year ago #

    Hi Shaun,

    Thank you so much, this really helped. I've tweaked it slightly as some parts weren't working for me (it was grabbing all the categories) and I changed the order to DESC.

    My only problem now is trying to get the post archive loop to not show the most recent post, before I was using the " 'offset' => 1 " but I can't seem to replicate that - any ideas?

    <!-- Featured Post -->
    <?php
        $args = array(
            'post__in'    => array(),
            'cat'    => get_query_var('cat'),
            'showposts' => 1,
    		'order' => 'DESC',
        );
        $featured = new WP_Query( $args );
        while( $featured->have_posts() ) : $featured->the_post();
    ?>
        <div class="arcfeaturepost">
         <h2><a class="highlight" href="<?php the_permalink();?>"><?php the_title(); ?></a></h2>
           <?php if ( has_post_thumbnail() ) { ?><div class="arcfeature"><a href="<?php the_permalink();?>"><?php the_post_thumbnail(); ?></a></div><?php } ?>
            <?php  the_excerpt('65');?>
            <p><a class="readmorea" href="<?php the_permalink();?>">READ MORE ></a></p>
        </div>
    <?php
    	endwhile;
    	wp_reset_postdata();
    ?>
    <!-- Post Archive Loop -->
    <?php
    while( have_posts() ) : the_post(); ?>
        <div class="arcpost">
            <div class="arcimg"><a href="<?php the_permalink();?>"><?php the_post_thumbnail(); ?></a></div>
            <div class="arctxt">
                <h3><?php the_title(); ?></h3>
                <?php  the_excerpt_dynamic(200,'no')?>
                <p><a class="readmorea" href="<?php the_permalink();?>">READ MORE ></a></p>
            </div>
        </div>
    <?php endwhile; ?>
    
    <!-- Page Navigation -->
    
    <?php if(function_exists('wp_paginate')) { wp_paginate(); } ?>

    Thank you for taking the time to do this, as you can tell I'm a bit of a PHP novice so I'm learning new approaches constantly!

    Edit: I should add that I tried adding an array before the 'while posts' for the second archive loop to try and add the offset bit there, but it didn't seem to work and it broke the pagination again.

  7. Moofin
    Member
    Posted 1 year ago #

    OOo, I figured it out. In the end I added:
    $c++; if( $c ==1 ) continue;

    So for those ever interested here is my final code:

    <!-- Featured Post -->
    <?php
        $args = array(
            'post__in'    => array(),
            'cat'    => get_query_var('cat'),
            'showposts' => 1,
            'order' => 'DESC',
        );
        $featured = new WP_Query( $args );
        while( $featured->have_posts() ) : $featured->the_post();
    ?>
        <div class="arcfeaturepost">
         <h2><a class="highlight" href="<?php the_permalink();?>"><?php the_title(); ?></a></h2>
           <?php if ( has_post_thumbnail() ) { ?><div class="arcfeature"><a href="<?php the_permalink();?>"><?php the_post_thumbnail(); ?></a></div><?php } ?>
            <?php  the_excerpt('65');?>
            <p><a class="readmorea" href="<?php the_permalink();?>">READ MORE ></a></p>
        </div>
    <?php
        endwhile;
        wp_reset_postdata();
    ?>
    <!-- Post Archive Loop -->
    <?php
    while( have_posts() ) : the_post(); $c++; if( $c ==1 ) continue; ?>
        <div class="arcpost">
            <div class="arcimg"><a href="<?php the_permalink();?>"><?php the_post_thumbnail(); ?></a></div>
            <div class="arctxt">
                <h3><?php the_title(); ?></h3>
                <?php  the_excerpt_dynamic(200,'no')?>
                <p><a class="readmorea" href="<?php the_permalink();?>">READ MORE ></a></p>
            </div>
        </div>
    <?php endwhile; ?>
    <!-- Page Navigation -->
    <?php if(function_exists('wp_paginate')) { wp_paginate(); } ?>

    Thank you so much for all the help Shaun - I definitely wouldn't have been able to do it without you!

  8. Shaun Scovil
    Member
    Posted 1 year ago #

    Glad I could help...and good work solving the rest of it!

Topic Closed

This topic has been closed to new replies.

About this Topic