WordPress.org

Forums

How to add unique id to only first post (4 posts)

  1. martcol
    Member
    Posted 4 years ago #

    I would like to add a unique id to the first post so that I can add a bit of margin-top to that one only.

    Thanks

    Martin

  2. esmi
    Forum Moderator
    Posted 4 years ago #

    <?php $c = 0;
    if (have_posts()) : while (have_posts()) : the_post()
    ;$c++;
    if( $c == 1 ) $my_id = ' id="first"';
    else $my_id = ''; ?>
    
    <div <?php post_class();?><?php echo $my_id;?>">
    <h2 class="post-title"><a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php _e('Post');?> <?php the_ID(); ?> - <?php _e('permanent link');?>"><?php the_title();?></a></h2>
  3. martcol
    Member
    Posted 4 years ago #

    Thank you esmi,

    For helping me with that.

    I had to chop it about a bit (I think) to make it fit my category.php template and this is how it ended up.

    <?php $c = 0;
    		if (have_posts()) : while (have_posts()) : the_post()
    			;$c++;
    		if( $c == 1 ) $my_id = ' id="first"';
    			else $my_id = ''; ?>
    <?php if ( in_category('3') ) { ?>
               <div class="post-cat-three"<?php echo $my_id;?> >
     <?php } else { ?>
               <div class="post" <?php echo $my_id;?>>
     <?php } ?>
     <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>

    Did I do anything wrong?

  4. esmi
    Forum Moderator
    Posted 4 years ago #

    Looks OK to me. Is it working?

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags