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'm new in wordpress thing and im trying to edit my posts. my problem is:

I'm editing posts in index.php

---------index.php----------

<?php get_header();?>
<div id="post-style">

    <h1 class="title"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h1>

    <?php the_content(); ?>

</div>



<?php endwhile; else: ?>

<h1 class="title">Ups...</h1>

<p>... pedimos desculpa mas nenhum <em>post</em> foi encontrado!</p>

<?php endif; ?>

<p align="center"><?php posts_nav_link(); ?></p>

---------end----------

I have 3 posts inline and they all have the same style except the middle one which I would like to be a little different.

share|improve this question
    
please post the full code of the loop of index.php. –  Michael Feb 22 '13 at 14:52
add comment

1 Answer

The $current_post property of the WP_Query object for the Loop should tell you which post you are on. Something like this should work:

<?php $pclass = ($wp_query->current_post === 1)  ? 'middle-post-class' : 'first-last-class' ; ?>
<div id="post-style" <?php post_class($pclass); ?> >

    <h1 class="title"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h1>

    <?php the_content(); ?>

</div>

The built in Loop counter-- $current_post-- starts from 0. That is why the second post is 1, and not 2.

Caveats: Your theme may already be using post_class somewhere else. You may not want to use it a second time. And if this is a custom WP_Query Loop the name of the object may be different-- that is, $my_query instead of wp_query, for example.

share|improve this answer
    
thank you done :) –  Andrew28 Feb 22 '13 at 15:02
    
+1. post_class() is the right answer. –  Chip Bennett Feb 22 '13 at 16:27
    
@Andrew28 if this solved the problem mark it as the accepted answer please. –  s_ha_dum Feb 22 '13 at 17:28
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.