• Luke

    (@lukejongibson)


    I only want the first header tag in the following code to be H1 and the following tags to be H2, I used to know how to do this, but I’ve complete forgotten. Can anyone point me in the right direction?

    Thank you

    <?php
    	$args = array( 'post_type' => 'discussion', 'posts_per_page' => 1 );
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post();
      echo '<div class="headline-discussion">';
      ?>
    
       <?php echo '<div class="headdisc-image">';
    
    if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
    	the_post_thumbnail();
    }
      echo '</div>';
    
      ?>
    
         <div class="post">
    
    				<h1 class="toptitle"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h1>
    
    			<div class="entry">
    				<?php the_excerpt(); ?>
    			</div>
    		</div>
    
      </div>
      <?php
    endwhile; ?>
Viewing 3 replies - 1 through 3 (of 3 total)
  • This might work:

    if( $wp_query->current_post == 0 && !is_paged() ) { /*first post*/ }

    Taken from this post

    based on @cindy Cullen‘s reply, and adapted to your custom query, try to replace this line:

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

    with (using a ternary operator):

    <<?php echo ( $loop->current_post == 0 ? 'h1' : 'h2' ); ?> class="toptitle"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h1>
    Thread Starter Luke

    (@lukejongibson)

    Excellent thank you. I will try these and let you know the results.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Loop – first header tag’ is closed to new replies.