WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'd like to display 9 posts and be able to insert a sticky post with its custom html template in between 4th & 5th post. Currently the sticky post gets displayed in both the "sticky container" and regular posts.

have_posts()) : $my_query->the_post(); ?>

<?php if ($count <= 9 ){ ;?>

      <?php echo get_the_content(); ?>

    <?php if ($count == 3) {
        $sticky = get_option('sticky_posts');
        $args = array(
            'posts_per_page' => 1,
            'post__in'  => $sticky,
            'ignore_sticky_posts' => 1

        if ( have_posts() ) : while ( have_posts() ) : the_post();

            if($sticky[0]) { ?> 
                <h1>CUSTOM HTML</h1>
                <?php echo get_the_content(); ?>
            endwhile; else:

<?php } $count++; ?>
<?php endwhile; ?>
share|improve this question
up vote 1 down vote accepted

Your code looks odd to me, typically to insert something between posts you add a counter and count each loop, then add custom code on a particular count, this is for the main loop, for a non main loop or secondary loop us wp_query.

<?php if (have_posts()) : ?>
<?php $count = 0; ?>

<?php query_posts( 'posts_per_page=9' );?>
<?php while (have_posts()) : the_post(); ?>

<?php $count++; ?>

<?php if ($count == 4) : ?>

 // Your custom sticky post and html goes here

<?php else : ?>

 // the regular loop code goes here such as the below

<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<?php the_excerpt(); ?>

<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
share|improve this answer

try to exclude the sticky post in the main loop:

global $query_string;
parse_str( $query_string, $args );
$args['post__not_in'] = get_option('sticky_posts'); 
query_posts( $args );
share|improve this answer

Your Answer


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.