Forums

Add custom class to individual recent posts (4 posts)

  1. shakingpaper
    Member
    Posted 3 years ago #

    Hi,

    I am creating a custom template. The front page has a 'recent news' section that requires the recent posts to be floated and therefore I need to be able to add a custom class to each of them (I can't use their id's as they will change as the posts get updated).

    My current loop code is below with the class news_post. I think the solution would be adding a duplicate loop that will show the next most recent story (everything else I have tried has resulted in a duplicate post).

    <!-- begin news loop -->
    		<h1>Latest News</h1>
    		<?php query_posts('cat=3&showposts=2'); ?>
    		<?php while (have_posts()) : the_post();
    		if( $post->ID == $do_not_duplicate ) continue; ?>
    
    		<div class="news_post" id="post-<?php the_ID(); ?>">
    		<h2><?php if (strlen($post->post_title) > 20) { echo substr(the_title($before = '', $after = '', FALSE), 0, 20) . '...';} else {the_title();} ?></h2>
    		<?php global $more; $more = 0; the_excerpt('Read more...'); ?>
    		</div>
    		<?php endwhile; ?>
    		<!-- end news loop -->

    Any help would be great

  2. Chris_K
    Member
    Posted 3 years ago #

    How about one "normal" loop with a counter in it and some if...then logic depending on how big the counter is?

    Like, if the counter is < 3 use different classes.

  3. shakingpaper
    Member
    Posted 3 years ago #

    I wouldn't have a clue where to start with that sort of stuff - could you be a bit more specific?

    Thanks for the reply!

  4. t31os
    Member
    Posted 3 years ago #

    If you want to make a counter, just doing something like this...

    $myvariable = 0;

    Then inside the loop do...

    $myvariable++;

    Then append whatever you want to add to with the variable...

    Example...

    <div class="myclass_'.$myvariable.'">example</div>

    Each result from the loop would then get...

    class="myclass_1" , class="myclass_2" ... and so on...

Topic Closed

This topic has been closed to new replies.

About this Topic