2

I've been trying to figure this out all day now.

Consider the following code below:

<div class="slider">
    <div class="slides"><?php
       wp_reset_query();
         if(have_posts()) : 
           while (have_posts()) : 
             the_post();
             if(is_sticky()) : ?>
               <div class="slide"><a rel="bookmark" href="<?php the_permalink() ?>"><?php echo the_post_thumbnail('full'); ?><h2><?php the_title(); ?></h2></a></div>
             <?php endif;
           endwhile; 
         else: ?>
           <p>Sorry, no posts matched your criteria.</p>
         <?php endif; ?>
      <div class="left-arrow"></div>
      <div class="right-arrow"></div>
  </div>
</div>

The loop above doesn't work on the site's pages (e.g: page 2/3/4) or 404.php, but works perfectly fine on page 1 (the index.php).

Why is that and how do I fix this?

2
  • 2
    Why the wp_reset_query()?
    – fuxia
    Mar 24, 2013 at 21:29
  • Nothing really. I thought that WordPress wasn't querying the right stuff, so I added it to double check. It functions the same without the wp_reset_query.
    – user28004
    Mar 24, 2013 at 21:30

2 Answers 2

4

If you look at that code carefully, the only thing it ever prints are sticky posts:

if(is_sticky()) : ?>
   <div class="slide"><a rel="bookmark" href="<?php the_permalink() ?>"><?php echo the_post_thumbnail('full'); ?><h2><?php the_title(); ?></h2></a></div>
<?php endif;

You stand a better chance of having sticky posts on page 1 of the index than on any page thereafter. 404 pages don't have posts at all, sticky or not. That is why they are 404 pages.

I think that is why the code appears not to work on some pages, but it isn't that the code is broken, just that the conditions for printing post data and not met.

5
  • Are you saying that the sticky posts are pulled from the current page? I specifically use a different loop than the one for pagination.
    – user28004
    Mar 24, 2013 at 21:52
  • Sticky posts are pushed (copied, I believe) to the beginning of the query results. I suppose you could have multiple pages of sticky posts but that seems unlikely. But also seems somewhat besides the point. Your Loop will only display sticky posts, and that means it won't print anything, chances are, for many of the pages on the site.
    – s_ha_dum
    Mar 24, 2013 at 21:57
  • Ah, but the point of sticky posts was intentional. The algorithm was planned to get all the sticky posts on the blog and display them in the slider. If I am understanding you correctly, my loop is actually incorrect in terms of its purpose, because you reference multiple pages of sticky posts, which is not what I was aiming for.
    – user28004
    Mar 24, 2013 at 22:02
  • You said that the Loop "doesn't work on the site's pages (e.g: page 2/3/4) or 404.php" and asked why. That is what I attempted to answer. You did not mention that this was a secondary loop, and really, it sounds like it isn't. You are trying to use the exiting query, which won't work. You should be using WP_Query to get your posts for this "sticky only" Loop.
    – s_ha_dum
    Mar 24, 2013 at 22:07
  • Thank you for your time. Your recommendation works perfectly. Regards,
    – user28004
    Mar 24, 2013 at 22:18
0

Remove the if(is_sticky()) and the endif code from the below snippet:

if(is_sticky()) : ?>
                <div class="slide"><a rel="bookmark" href="<?php the_permalink() ?>"><?php echo the_post_thumbnail('full'); ?><h2><?php the_title(); ?></h2></a></div>
  <?php endif;

This conditional statement tells WordPress only to run if the post is sticky. Typically sticky posts appear on page 1 only.

Your Answer

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