0

I have a situation where I want the latest post to display with a larger image and more text than the recent posts which would display thereafter.

So the most recent post might show about 500 characters of text and a large image. And any post thereafter would only have the thumbnail image and display 140 characters. This is the code for my posts currently:

<?php $query1 = new WP_Query( 'post_type=post&posts_per_page=1' );

while ( $query1->have_posts() ) {
$query1->the_post();
echo '<h4>' . get_the_title( $query1->post->ID ) . '</h4>';
echo '<div class="">' . get_the_post_thumbnail( $query1->post->ID, 'wpbs-featured-post' ) . '</div>';
echo '<div class="">' . get_the_content( $query1->post->ID ) . '</div>';

}
wp_reset_postdata();

$query2 = new WP_Query( 'post_type=post&posts_per_page=100&offset=1' );

while( $query2->have_posts() ) {
$query2->next_post();
echo '<h4>' . get_the_title( $query2->post->ID ) . '</h4>';
echo '<div class="">' . get_the_post_thumbnail( $query2->post->ID , 'thumbnail' ) .   '</div>';
echo '<div class="">' . get_the_excerpt( $query2->post->ID ) . '</div>';
}

wp_reset_postdata(); ?> 

Is this possible?

0

You need 2 queries e.g.

$query1 = new WP_Query( 'post_type=post&posts_per_page=1' );

// The Loop
while ( $query1->have_posts() ) {
    $query1->the_post();
    echo '<li>' . get_the_title() . '</li>';
}
wp_reset_postdata();

$query2 = new WP_Query( 'post_type=post&posts_per_page=100&offset=1' );

while( $query2->have_posts() ) {
    $query2->next_post();
    echo '<li>' . get_the_title( $query2->post->ID ) . '</li>';
}

wp_reset_postdata();

The first query gets the first post (which you can then do what you want) and the second query gets the rest of the posts but skips the first post (from the first query) using the offset tag.

  • be aware that using 'offset' is going to make problems if you need pagination. – Michael May 28 '14 at 19:52
  • That's true Michael, there is a fix available at codex.wordpress.org/… – Gareth Gillman May 28 '14 at 19:54
  • I've added in the title and thumbnails etc. Those seem to be working well. I've added get_the_content for the first one and get_the_excerpt for the rest. For some reason the content from the first blog post is going in to all the excerpts thereafter. I'll edit the code in my question above to show it – Ciarán May 28 '14 at 20:42
  • @all, Need some help here. Where do I need to write this code? – Vikas Yadav May 23 '18 at 6:48

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.