The Stack Overflow podcast is back! Listen to an interview with our new CEO.
1 of 4

$count not working in wordpress loop (twentyeleven style)

I have used $count before, but never run into this problem. I cannot get it to work. I have limited php background, so please be patient with me! Here is the $count setup code:

<?php $count = 0; ?>
<?php if ( have_posts() ) : // Start the Loop ?>
    <?php $count++; ?>      
    <?php while ( have_posts() ) : the_post(); ?>
        <?php get_template_part( 'content', get_post_format() ); ?>
    <?php endwhile; ?>  
<?php endif; ?>

Within content.php - here is what I am trying to do:

<?php if ($count == 1) { ?>
    <section class="stories-title">
        <img src="<?php bloginfo('stylesheet_directory'); ?>/assets/stories-title.jpg" />
    </section>
<?php ;} ?>

Can anyone explain to me what's happening? To the best of my knowledge, I'm running into an undefined variable issue, but I'm not sure how to fix this without breaking the $count... do the conditional statements and content have to be in the same file for $count to work?

Any help would be greatly appreciated!