So, I am in a bind and in the last moments of finishing a theme. However, I thought I would ask someone if they can whip up a multiple loop for the front page that was a last minute request by my client.
I do not need anything but the PHP, but please be decent enough to label what is happening in the multiple loop
What I need is
1. Most recent post and 2 more would be an excerpt with 150 length with a thumbnail (please note I already know how to integrate my own,so just put a hidden code piece with thumbnailcodehere as a placer.
2. The next 5 would be just be Title, thumbnail, and the read more link... no excerpt.
------------
Linda answers:
Hi, try something like this...
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php query_posts('showposts=1'); ?>
<?php $posts = get_posts('numberposts=1&offset=0'); foreach ($posts as $post) : start_wp(); ?>
<?php static $count1 = 0; if ($count1 == "1") { break; } else { ?>
<?php the_title(); ?>
<?php the_content(); ?> // display the full content of the first post only
<?php $count1++; } ?>
<?php endforeach; ?>
<?php query_posts('showposts=2'); ?>
<?php $posts = get_posts('numberposts=2&offset=1'); foreach ($posts as $post) : start_wp(); ?>
<?php static $count2 = 0; if ($count2 == "2") { break; } else { ?>
<?php the_title(); ?> <?php the_excerpt(); ?> // display the excerpt of the next two
<?php $count2++; } ?>
<?php endforeach; ?>
<?php query_posts('showposts=8'); ?>
<?php $posts = get_posts('numberposts=8&offset=3'); foreach ($posts as $post) : start_wp(); ?>
<?php static $count3 = 0; if ($count3 == "8") { break; } else { ?>
<a class="" href="<?php the_permalink(); ?>"><?php the_title(); ?></a> // display the last as just title and link
<?php $count3++; } ?>
<?php endforeach; ?>
<?php endwhile; ?>
<?php else : ?>
<?php endif; ?>
You might also want to increase your bid for more answers. :)
Cheers!
Nile Flores comments:
This seems to spit out unnecessary code and is not what I was asking for.
Kannan C answers:
<?php
// The Query for first 3 recent posts
$the_query = new WP_Query( array( 'post_type' => 'post', 'orderby' => 'date', 'order' => 'DESC', 'posts_per_page' => '3' ) );
// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();
echo '<div>'.the_title();
echo get_the_post_thumbnail('thumbnail', array('class' => 'alignleft') );
echo the_excerpt_max_charlength(150);
echo '</div>';
endwhile;
// Reset Post Data
wp_reset_postdata();
?>
The next five recent posts
<?php
// The Query for 4 to 9 recent posts
$the_query = new WP_Query( array( 'post_type' => 'post','orderby' => 'date', 'order' => 'DESC', 'offset=3', 'posts_per_page' => '5' ) );
// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();
echo '<div>'.the_title();
echo get_the_post_thumbnail('thumbnail', array('class' => 'alignleft') );
echo '<a href="'.get_permalink().'">Read more</a>';
echo '</div>';
endwhile;
// Reset Post Data
wp_reset_postdata();
?>
The following lines to be go on your functions.php file
add_theme_support( 'thumbnail' ); //to support featured thumbnail
function the_excerpt_max_charlength($charlength) {
$excerpt = get_the_excerpt();
$charlength++;
if(strlen($excerpt)>$charlength) {
$subex = substr($excerpt,0,$charlength-5);
$exwords = explode(" ",$subex);
$excut = -(strlen($exwords[count($exwords)-1]));
if($excut<0) {
echo substr($subex,0,$excut);
} else {
echo $subex;
}
echo "[...]";
} else {
echo $excerpt;
}
}
ej_emman answers:
Hello Nile,
Maybe you can try this...
//Place this into your function.php,. this will filter the excerpt length
function new_excerpt_length($length) {
return 150;
}
add_filter('excerpt_length', 'new_excerpt_length');
//multi loops start here. this will query on the condition given base on what you need.
// it works on me. hope this helps..
<?php
$multi_loops = new WP_Query();
$multi_loops->query('showposts=7');
$i = 0;
?>
<?php while ($multi_loops->have_posts()) : $multi_loops->the_post(); ?>
<li>
<?php if($i > 2): ?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php if(has_post_thumbnail)
echo the_post_thumbnail(); ?>
<a href="<?php the_permalink() ?>"> read more...</a>
<?php else:
if(has_post_thumbnail)
echo the_post_thumbnail();
the_excerpt();
endif;
$i++;
?>
</li>
<?php endwhile; ?>