我可以毫无问题地用不同的风格写前三篇文章。
我不明白的是,如何做到这一点,并将前三篇文章包含在一个容器div中(例如,每个文章都没有新的div)。
输出是这样的:(但很明显,这将循环容器,我不希望这样…)
<?php
$wp_query = new WP_Query();
$wp_query->query(array(
\'post_type\'=>\'post\',
));
$i = 1; // counter to style first 3 different
if ($wp_query->have_posts()) : ?>
<?php while ($wp_query->have_posts()) : ?>
<?php $wp_query->the_post(); ?>
<?php
if($i < 4) { // first 3 posts ?>
<div class="container">
<div class="post">
<?php the_title(); ?>
</div>
</div>
<?php } else { // not the first 3 posts ?>
<div class="differnt-post-style">
<?php the_title(); ?>
</div>
<?php } ?>
<?php endwhile; endif; ?>
最合适的回答,由SO网友:Privateer 整理而成
正如米洛所指出的,最终结果可能如下所示
if ($wp_query->have_posts()) {
while ($wp_query->have_posts()) {
$wp_query->the_post();
$post_title = get_the_title();
# On the first pass, write the start of your single container
if ( 0 == $wp_query->current_post ) {
echo \'<div class="container">\';
}
echo <<<HTML
<div class="different-post-style">
{$the_title}
</div>
HTML;
# On the 3rd pass, close your single container
if ( 2 == $wp_query->current_post ) {
echo "\\r\\n</div>";
}
}
# Close the div if there were 2 or fewer posts
if ( $wp_query->current_post < 2 ) {
echo "\\r\\n</div>";
}
}
根据米洛的意见进行修改。我没有想到这一点。