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'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?

share|improve this question
2  
Why the wp_reset_query()? –  toscho Mar 24 '13 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. –  railgun Mar 24 '13 at 21:30
add comment

2 Answers 2

up vote 4 down vote accepted

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.

share|improve this answer
    
Are you saying that the sticky posts are pulled from the current page? I specifically use a different loop than the one for pagination. –  railgun Mar 24 '13 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 '13 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. –  railgun Mar 24 '13 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 '13 at 22:07
    
Thank you for your time. Your recommendation works perfectly. Regards, –  railgun Mar 24 '13 at 22:18
add comment

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.

share|improve this answer
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.