0

I'm trying to make a Wordpress blog where the home page shows only the 3 most recent posts, but for the newest of these posts, it shows the entire post (not just the teaser content), and then for the other 2, it shows the teaser content with the "read more" button.

I'm currently building all this with the "Basic" theme.

I'm still fairly new to Wordpress and PHP, but have a firm background in HTML/CSS, and a little Java background.

Any ideas?

Also, is there a way to control how much of the post is shown before the "Read More" button?

1

Add a counter to the query and vary the output depending upon what the count is.

You'll need to either directly edit the args to limit to 3 posts_per_page or use pre_get_posts to do it.

pre get posts example (into your functions.php)

function hwl_home_pagesize( $query ) {
    if ( is_home() ) {
        // Display only 3 post for the original blog archive
        $query->set( 'posts_per_page', 3 );
        return;
    }

}
add_action( 'pre_get_posts', 'hwl_home_pagesize', 1 );

Then into your home.php (or whichever file basic theme uses):

if ( have_posts() ) {
    $i=1;
    while ( have_posts() ) {
        the_post(); 
        //
        if ($i==1) {
        //first post
           the_title();
           the_content();
        }else{
            //other 2 posts
           the_title();
           the_excerpt();
        }
        //
        $i++;
    } // end while
} // end if

Now in your dashboard also make sure that settings/reading the option "show full text" is selected.

Or, if you want to start your own query you can instead of using the main query:

// WP_Query arguments
$args = array( 'posts_per_page' => '3'; 'post_type' => 'posts';
);

// The Query
$query = new WP_Query( $args );

// The Loop
if ( $query->have_posts() ) {
  $i=1;
while ( $query->have_posts() ) {
    $query->the_post();
    // do something
        if ($i==1) {
        //first post
           the_title();
           the_content();
        }else{
        //other 2 posts
           the_title();
           the_excerpt();
        }
   $i++;

}

    } else {
    // no posts found
    }
// Restore original Post Data
wp_reset_postdata();

THis would eliminate the need to add to your functions.php

| improve this answer | |
0

for the first part of your question i would use this to achieve it

   // FIRST LOOP: display posts 1 
query_posts('showposts=1'); 
 $posts = get_posts('numberposts=1&offset=0'); foreach ($posts as $post) : start_wp(); 
 static $count1 = 0; if ($count1 == "1") { break; } else { 

the_title(); the_content();

$count1++; } endforeach;

// SECOND LOOP: display posts 2 and 3 query_posts('showposts=2'); $posts = get_posts('numberposts=2&offset=1'); foreach ($posts as $post) : start_wp(); static $count2 = 0; if ($count2 == "2") { break; } else {

the_title(); the_excerpt();

$count2++; } endforeach;

For the second part of you question on showing how much is show before the read more, add this to your function file

function new_excerpt_length($length) { return 15; } add_filter('excerpt_length', 'new_excerpt_length');

change the number 15 to the amount you like to be shown

| improve this answer | |
0

You can set posts per page from wordpress admin pannel Settings > Reading settings > Blog pages show at most, set to whatever you need.

$args = array(
    'posts_per_page'   => 3,
    'orderby'          => 'date',
    'order'            => 'DESC',
    'post_type'        => 'post',
    'post_status'      => 'publish',
);
$posts_array = get_posts( $args );
echo "<ul>";
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
    <li>
        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
         <?php the_content();?>
    </li>
<?php endforeach; 
wp_reset_postdata();?>
echo "</ul>";
| improve this answer | |
  • Ok, I get that. But that doesn't answer the desire for it to show the full post of the first one, then the shortened versions of the second and third. – Justgrant2009 Apr 12 '17 at 3:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.