• Resolved jimmiejo

    (@jimmiejo)


    I’ve read through plenty of threads on the forum, and there doesn’t seem to be a precise answer to my question/issue:

    What’s the best way to apply different formatting to just the first post on the first page, without using wp_query, offset, and multiple loops?

    I’ve seen ways to apply additional styling to just the first post on the first page (https://www.ads-software.com/support/topic/293739?replies=5), but not to change the code/format of the post.

    What I mean is:

    <?php if (have_posts()) : ?>
    	<?php $post = $posts[0]; ?>
    	<?php while (have_posts()) : the_post(); ?>
    
    // what would the code be for "if is the first post" so it shows:
    
    <h1>The first post on the main index page</h1>
    <?php the_title(); ?>
    <?php the_excerpt(); ?>
    
    // what would the code be for "if is every other post thereafter the first on the first page" so it shows:
    
    <h2><?php the_title(); ?></h2>
    <?php the_content(); ?>
    
    <?php endwhile; ?>
    
    	<!-- page nav -->
    
    <?php endif; ?>

    Thanks in advance, guys.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Try:

    <?php if (have_posts()) : ?>
    <?php $post = $posts[0]; $c=0;?>
    <?php while (have_posts()) : the_post(); ?>
    
    <?php $c++;
    if( $c == 1) :?>
    <h1>The first post on the main index page</h1>
    <?php the_title(); ?>
    <?php the_excerpt(); ?>
    
    <?php else :?>
    <h2><?php the_title(); ?></h2>
    <?php the_content(); ?>
    <?php endif;?>
    
    <?php endwhile; ?>
    
    <!-- page nav -->

    <?php endif; ?>`

    Thread Starter jimmiejo

    (@jimmiejo)

    Thanks esmi. That works in using custom formatting for the first post, but applies it to the second, third, etc.. pages thereafter for the first post on the page.

    I need it to only apply to the first post on the first page (ie. index, not /page/2).

    Appreciate your help.

    Try like that. Use $paged.

    <?php if (have_posts()) : ?>
    <?php $post = $posts[0]; $c=0;?>
    <?php while (have_posts()) : the_post(); ?>
    
    <?php $c++;
    if( !$paged && $c == 1) :?>
    <h1>The first post on the main index page</h1>
    <?php the_title(); ?>
    <?php the_excerpt(); ?>
    
    <?php else :?>
    <h2><?php the_title(); ?></h2>
    <?php the_content(); ?>
    <?php endif;?>
    
    <?php endwhile; ?>
    
    // page nav
    <?php endif; ?>

    Thread Starter jimmiejo

    (@jimmiejo)

    That did the trick! Thanks yakuphan and esmi!

    bencharity

    (@bencharity)

    If I want to pull only from a certain category, where would I put that call?

    Using this:

    <?php if (have_posts()) : ?>
    <?php $post = $posts[0]; $c=0;?>
    <?php while (have_posts()) : the_post(); ?>
    
    <?php $c++;
    if( $c == 1) :?>
    <h1>The first post on the main index page</h1>
    <?php the_title(); ?>
    <?php the_excerpt(); ?>
    
    <?php else :?>
    <h2><?php the_title(); ?></h2>
    <?php the_content(); ?>
    <?php endif;?>
    
    <?php endwhile; ?>

    I was using this:

    <?php
    query_posts("cat=16");
    global $more;
    // set $more to 0 in order to only get the first part of the post
    $more = 0;
    ?>

    How would I call category 16 in the first code?

    Thanks again!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Applying different formatting to just the first post on the first page’ is closed to new replies.