0

I have Around 40 Excerpts on my homepage (I am using Gantry Framework) and I want to style each of these Excerpts in a different Manner. i.e Different Backgrounds, Font color For Every Excerpt.

So How can I add custom Class to each and every excerpt which is there on my homepage...say in manner Excerpt1 , Excerpt2 .... Excerpt40.

This is what I have explored so far: These Excerpts Are being called in a file content-blog.php like this!!

<div class="post-content">
  <?php if( $gantry->get( 'blog-content', 'content' ) == 'excerpt' ) : ?>
    <?php the_excerpt(); ?>
  <?php else : ?>
    <?php the_content( false ); ?>   
  <?php endif; ?>
</div>

So If I do this:

<div class="post-content">
    <?php if( $gantry->get( 'blog-content', 'content' ) == 'excerpt' ) : ?>
      <div class="sameExcerpt">
        <?php the_excerpt(); ?>
      </div>
    <?php else : ?>
      <?php the_content( false ); ?>
    <?php endif; ?>
</div>

A single class i.e sameexcerpt will be assigned to each excerpt. But if can somehow use a global variable or static variable.

and keep incrementing that variable like this:

<div class="sameExcerpt<php echo ++globalvariable">
  <?php the_excerpt(); ?>
</div>  
<?php else : ?>
  <?php the_content( false ); ?>     
<?php endif; ?>
</div>

Then I think I can Assign a different Class to Every Excerpt.

Any Help Will Be greatly Appreciated.

1 Answer 1

1

Firstly, there are syntax errors in your (last block of) code.

And secondly, you don't need to add any specific class at all. Just use the post ID for each post:

<article id="post-<?php the_ID(); ?>">
    <div class="post-content">
        <?php if ('excerpt' === $gantry->get('blog-content', 'content')) : ?>
            <div class="excerpt">
                <?php the_excerpt(); ?>
            </div>
        <?php else : ?>
            <?php the_content(false); ?>
        <?php endif; ?>
    </div>
</article>

Then you can target your excerpts like so:

#post-23 .excerpt {
    /* styles for excertp of post 23 */
}

#post-42 .excerpt {
    /* styles for excertp of post 42 */
}

But, why would you want to individually target each and every excerpt in the first place?

This is unfeasible for a large number of posts. And your website might look a little chaotic, too.

6
  • I have already tried '#post-312 .excerpt{ }' and '#post-312 {}', both of them are not working. I want to target only 10 excerpts and i thought the only way out is to add custom classes to each and every excerpt!! Actually i want to show a few quotes on these excerpts , and i want to style each and every quotes in a different manner. It will look nice!
    – bhavya_w
    Commented Feb 13, 2014 at 9:38
  • Http://www.lifathon.com
    – bhavya_w
    Commented Feb 13, 2014 at 9:56
  • The above would be the only logical way within wordpress to do this, unless you where to create a function that randomly generates class names and contains 1 - 40 as an interger you could then append the two together.
    – dannyw24
    Commented Feb 13, 2014 at 10:01
  • There are several errors in your HTML. For instance, you should use esc_attr for your title attribute values. And the post with ID 312 doesn't even have an excerpt. Are you sure you are trying to target the right post, and/or added the above code at the right place?
    – tfrommen
    Commented Feb 13, 2014 at 10:02
  • 1
    Thanks a lot Guys... It worked, There was some problem with css rendering...its working by the above method..Anyways i learned how to use global variables in wordpress because of this....thanks again..
    – bhavya_w
    Commented Feb 13, 2014 at 10:29

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.