• I’ve had a look through the codex at the documentation for the loop and for sticky posts and I can’t quite figure out what my issue is.

    What I’m trying to accomplish is on the FIRST page of index.php, I want to show ONE sticky post IF there is one, then the first post formatted with class=”first-post”, then the remaining posts formatted with class=”even” or class=”odd”. For subsequent pages using index.php, I just want to show class=”odd” or class=”even”.

    The even/odd and first-post stuff is working fine EXCEPT if I have a sticky post published. IF I have a sticky post published, the homepage (first page using index.php) shows the sticky post and NOTHING else. If I un-publish the sticky post, everything works fine.

    Can someone help me? Below is the code I’m using:

    <?php
    	$sticky = get_option( 'sticky_posts' );
    	$args = array(
    		'posts_per_page' => 1,
    		'post__in'  => $sticky,
    		'ignore_sticky_posts' => 1
    	);
    	$stickyquery = new WP_Query( $args );
    	// if ( $sticky[0] ) {
    	// 	// insert here your stuff...
    	// }
    ?>
    <?php if ($stickyquery->have_posts()) : ?>
    	<?php while ($stickyquery->have_posts()) : $stickyquery->the_post(); ?>
    		<div id="post-<?php the_ID(); ?>" class="sticky-post">
    			<div class="entry">
    				<?php the_content(''); ?>
    			</div>
    		</div>
    	<?php endwhile; ?>
    <?php endif; wp_reset_query(); ?>
    
    <?php
    	// Remaining non-sticky post(s)
    	$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
    	$sticky = get_option( 'sticky_posts' );
    	$args = array(
    		'ignore_sticky_posts' => 1,
    		'post__not_in' => $sticky,
    		'paged' => $paged
    	);
    	$query = new WP_Query( $args );
    ?>
    <?php if ($query->have_posts()) : ?>
    	<?php $postcount=0; ?>
    	<?php if (is_paged()) {$postcount++; } ?>
    	<?php while ($query->have_posts()) : $query->the_post(); ?>
    
    		<?php if ($postcount == 0) : // First post on first page ?>
    			<div id="post-<?php the_ID(); ?>" class="first-post clearfix">
    				<div class="featured-image">
    					<?php if ( has_post_thumbnail() ) { the_post_thumbnail('large'); } ?>
    				</div>
    				<h1 class="entry-title">
    					<a href="<?php the_permalink() ?>" rel="bookmark" title="Permalink"><?php the_title(); ?></a>
    				</h1>
    				<div class="entry">
    					<?php echo excerpt(55); ?>
    				</div>
    			</div>
    			<?php $postcount++; // increment after the first post is identified ?>
    
    		<?php else : ?>
    			<div id="post-<?php the_ID(); ?>" class="post <?php echo ($postcount % 2 == 0 ? 'even' : 'odd' ); ?>">
    				<div class="featured-image">
    					<?php if ( has_post_thumbnail() ) { the_post_thumbnail('medium'); } ?>
    				</div>
    				<h1 class="entry-title">
    					<a href="<?php the_permalink() ?>" rel="bookmark" title="Permalink"><?php the_title(); ?></a>
    				</h1>
    				<div class="entry">
    					<?php echo excerpt(55); ?>
    				</div>
    			</div>
    			<?php $postcount++; // increment for remaining posts ?>
    		<?php endif; ?>
    	<?php endwhile; ?>
    
    <div id="post-nav" class="clearfix clear">
    	<?php posts_nav_link('','<div class="alignright">Newer >></div>','<div class="alignleft"><< Older</div>'); ?>
    </div>
    
    <?php else : ?>
    	<div class="post">
    		<h1 class="entry-title">Not Found</h1>
    		<div class="entry">
    			<p>Sorry, but you are looking for something that isn't here.</p>
    		</div>
    	</div>
    <?php endif; wp_reset_query(); ?>
Viewing 1 replies (of 1 total)
  • Don’t know what is going on with your site – the code works fine for me. Maybe a plugin interfering, or something in your theme.

Viewing 1 replies (of 1 total)
  • The topic ‘Issues with multiple loops and sticky posts on index.php’ is closed to new replies.