WordPress.org

Ready to get started?Download WordPress

Forums

Making the most recent post stand out (4 posts)

  1. pshero
    Member
    Posted 5 years ago #

    Hey gang, I'm hoping one of you PHP gurus will be able to give me a little assistance. On my website (pshero.com) I use a series of custom fields to create summaries of each of my posts like this:

    '<?php while ( have_posts() ) : the_post() ?>

    <!-- GET ARTICLE_IMAGE CUSTOM FIELD -->

    <div class="article_preview">
    <?php //get article_image (custom field) ?>
    <?php $image = get_post_meta($post->ID, 'article_image', true); ?>
    "><img src="<?php echo $image; ?>" alt="<?php the_title(); ?>" />

    <h1>
    " title="<?php printf(__('Permalink to %s', 'sandbox'), wp_specialchars(get_the_title(), 1)) ?>" rel="bookmark"><?php the_title() ?>
    </h1>

    <small>
    <?php the_category(',') ?>  //  <?php comments_popup_link('0', '1', '%'); ?>
    </small>

    <div class="content">
    <p>
    <?php //get article_summary (custom field) ?>
    <?php $summary = get_post_meta($post->ID, 'article_summary', true); ?>
    <?php echo $summary; ?>
    </p>
    </div>

    <div style="clear:both"></div>

    </div><!-- /#article_preview -->

    <div class="article_separator"></div>

    <!-- .post -->

    <?php comments_template() ?>

    <?php endwhile; ?>'

    But what I want to do is find a way to style only the most recent post (on the homepage only) differently than the rest. I've scoured the net and found several snippets of code that in theory should do the trick, but they all end up breaking the page completely.

    I'm sure there are others out there with the same wish, so any help would be greatly appreciated!

    Thanks in advance.

  2. brankoa
    Member
    Posted 5 years ago #

    Hi there,

    You could do it like
    IF POST IS FIRST SET SOME CSS STYLE, ELSE DO NOT SET ANY SPECIAL STYLE

    Here is how

    $postcount = 0;
    if (have_posts()) { while (have_posts()) { the_post();
    if( $postcount == 0 ) {
    // SET SOME CSS TO SOME POST WRAPPING DIV ON THE CODE THAT CAN BE THE SAME LIKE THE ONE FROM index.php

    }

    else { HERE YOU CAN COPY PASTE ENTIRE ORIGINAL index.php code }
    $postcount ++; /* counter is in while loop */
    } /* closes while loop */
    } /* closes if $postcount == 0 */
    } /* closes if have_posts() */

  3. pshero
    Member
    Posted 5 years ago #

    Thanks Brankoa, I'll give that a shot and report back.

  4. pshero
    Member
    Posted 5 years ago #

    Hmmm... Kills my whole page. Not sure what I'm doing wrong here. I don't usually have problems implementing php, just crap at writing it. Here's my simplified page... can you see the problem?

    '<?php get_header() ?>

    <div id="content">

    <!-- .post -->
    <?php while ( have_posts() ) : the_post() ?>

    <!-- GET ARTICLE_IMAGE CUSTOM FIELD -->

    <?php $postcount = 0;
    if (have_posts()) { while (have_posts()) { the_post();
    if( $postcount == 0 ) { ?>

    <div class="article_preview">
    <?php //get article_image (custom field) ?>
    <?php $image = get_post_meta($post->ID, 'article_image', true); ?>
    "><img src="<?php echo $image; ?>" alt="<?php the_title(); ?>" />

    <h1>
    " title="<?php printf(__('Permalink to %s', 'sandbox'), wp_specialchars(get_the_title(), 1)) ?>" rel="bookmark"><?php the_title() ?>
    </h1>

    <small>
    <?php the_category(',') ?>  //  <?php comments_popup_link('0', '1', '%'); ?>
    </small>

    <div class="content">
    <p>
    <?php //get article_summary (custom field) ?>
    <?php $summary = get_post_meta($post->ID, 'article_summary', true); ?>
    <?php echo $summary; ?>
    </p>
    </div>

    <?php } ?>

    <?php else { ?>

    <div class="article_preview">
    <?php //get article_image (custom field) ?>
    <?php $image = get_post_meta($post->ID, 'article_image', true); ?>
    "><img src="<?php echo $image; ?>" alt="<?php the_title(); ?>" />

    <h1>
    " title="<?php printf(__('Permalink to %s', 'sandbox'), wp_specialchars(get_the_title(), 1)) ?>" rel="bookmark"><?php the_title() ?>
    </h1>

    <small>
    <?php the_category(',') ?>  //  <?php comments_popup_link('0', '1', '%'); ?>
    </small>

    <div class="content">
    <p>
    <?php //get article_summary (custom field) ?>
    <?php $summary = get_post_meta($post->ID, 'article_summary', true); ?>
    <?php echo $summary; ?>
    </p>
    </div>

    <?php } ?>
    <?php $postcount ++; ?>
    <?php } ?>
    <?php } ?>
    <?php } ?>

    <div style="clear:both"></div>

    </div><!-- /#article_preview -->

    <div class="article_separator"></div>

    <!-- .post -->

    <?php comments_template() ?>

    <?php endwhile; ?>

    <div id="nav_below" class="navigation">
    <?php if(function_exists('wp_pagenavi')) { wp_pagenavi(); } ?>
    </div>

    </div><!-- #content -->

    <div id="sidebar">
    <?php get_sidebar() ?>
    </div>

    <?php get_footer() ?>'

Topic Closed

This topic has been closed to new replies.

About this Topic