WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Applying different formatting to just the first post on the first page (6 posts)

  1. jimmiejo
    Member
    Posted 4 years ago #

    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 (http://wordpress.org/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.

  2. esmi
    Forum Moderator
    Posted 4 years ago #

    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; ?>`

  3. jimmiejo
    Member
    Posted 4 years ago #

    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.

  4. yakuphan
    Member
    Posted 4 years ago #

    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; ?>
  5. jimmiejo
    Member
    Posted 4 years ago #

    That did the trick! Thanks yakuphan and esmi!

  6. bencharity
    Member
    Posted 4 years ago #

    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!

Topic Closed

This topic has been closed to new replies.

About this Topic