0

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.

1
  • please post the full code of the loop of index.php.
    – Michael
    Commented Feb 22, 2013 at 14:52

1 Answer 1

2

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.

2
  • +1. post_class() is the right answer. Commented Feb 22, 2013 at 16:27
  • @Andrew28 if this solved the problem mark it as the accepted answer please.
    – s_ha_dum
    Commented Feb 22, 2013 at 17:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.