WordPress.org

Ready to get started?Download WordPress

Forums

Custom most recent without loop (4 posts)

  1. sf23103
    Member
    Posted 2 years ago #

    I am building a custom wordpress theme, and am wondering if this is possible:

    The client wants the 6 most recent articles (Title, date, author, and excerpt (certain number of characters). The issue is, each of the 6 most recent articles will be in different DIV's and there is no way for me to set it up as a loop in that design.

    Has anyone produced any code that will allow me to put the most recent article in one box, the second in another, third, and so on without doing one loop? I would assume it would be possible to print "Most Recent Article 1" here, "Second Most Recent Article" there, etc.

    Ideas?

  2. alchymyth
    Forum Moderator
    Posted 2 years ago #

    each of the 6 most recent articles will be in different DIV's and there is no way for me to set it up as a loop in that design.

    generally, this should be no problem.

    what do you have so far as code for the recent articles?

    (please use the pastebin for the code - see http://codex.wordpress.org/Forum_Welcome#Posting_Code )

  3. Digital Raindrops
    Member
    Posted 2 years ago #

    Code snippet for content.php in a twenty eleven child theme!

    <?php
    	global $wp_query;
    	$divisor = 6;
    	$counter = ($wp_query->current_post + 1) % $divisor;
    	$class = $counter ? 'recent-' .$counter : 'recent-' .$divisor;
    ?>
    
       <article id="post-<?php the_ID(); ?>" <?php post_class( $class ); ?>>

    Based on your posts per page $wp_query->current_post will start at zero, so ten posts returns 0 - 9

    Set the $divisor to the number of styles, you want 6 others might want two for columns.

    % $divisor; will return 0 if the counter is divisable.
    So we get 1, 2, 3, 4, 5, 0, 1, 2, 3, 4

    $class = $counter ? 'recent-' .$counter : 'recent-' .$divisor;

    This will concatenate 'recent-' and $counter if $counter != 0, if $counter == 0 it will concatenate 'recent-' and $divisor

    recent-1, recent-2, recent-3, recent-4, recent-5 and recent-6

    Last we set our class with the post class post_class( $class );
    WordPress will append our class to the post classes!

    <article id="post-1" class="post-1 post type-post status-publish format-standard hentry category-uncategorized recent-1">

    Then in the style.css

    .recent-1 {
    
    }
    
    .recent-2 {
    }

    Pretty Neat!

    HTH

    david

  4. sf23103
    Member
    Posted 2 years ago #

    Way cool! Thanks so much. I'm definitely pretty new to WordPress (and PHP in general) so this might take me some time, that's a huge help. I'll let you know how it turns out.

    Thanks for your time!!

    Alex

Topic Closed

This topic has been closed to new replies.

About this Topic