admin管理员组文章数量:1389860
I need to display all the months that have active posts, and inside each month I need to display at least 5 posts that are tied to each month.. the HTML will look like this:
News items<h6>May</h6>
<ul>
<li><a href="#">Souvlaki ignitus carborundum</a></li>
<li><a href="#">Defacto lingo est igpay atinlay</a></li>
<li><a href="#">Quote meon an estimate</a></li>
<li><a href="#">Souvlaki ignitus carborundum</a></li>
<li><a href="#">Defacto lingo est igpay atinlay</a></li>
<li><a href="#">Quote meon an estimate</a></li>
</ul>
<h6>April:</h6>
<ul>
<li><a href="#">Sic tempus fugit esperanto hiccup</a></li>
<li><a href="#">Epsum factorial non deposit</a></li>
</ul>
Not sure how I can go about this or what function to use.. any help/direction would be appreciated.
I need to display all the months that have active posts, and inside each month I need to display at least 5 posts that are tied to each month.. the HTML will look like this:
News items<h6>May</h6>
<ul>
<li><a href="#">Souvlaki ignitus carborundum</a></li>
<li><a href="#">Defacto lingo est igpay atinlay</a></li>
<li><a href="#">Quote meon an estimate</a></li>
<li><a href="#">Souvlaki ignitus carborundum</a></li>
<li><a href="#">Defacto lingo est igpay atinlay</a></li>
<li><a href="#">Quote meon an estimate</a></li>
</ul>
<h6>April:</h6>
<ul>
<li><a href="#">Sic tempus fugit esperanto hiccup</a></li>
<li><a href="#">Epsum factorial non deposit</a></li>
</ul>
Not sure how I can go about this or what function to use.. any help/direction would be appreciated.
Share Improve this question asked Jul 10, 2012 at 20:56 user990717user990717 1532 silver badges8 bronze badges 2- can you give more details about the 'at least 5 posts'? – Michael Commented Jul 10, 2012 at 21:55
- i need to display the latest 5 posts from each month – user990717 Commented Jul 10, 2012 at 22:59
2 Answers
Reset to default 3one possibility is to run all posts through the loop and only output the month once per new month:
<?php
$counter = 0;
$ref_month = '';
$monthly = new WP_Query(array('posts_per_page' => -1));
if( $monthly->have_posts() ) : while( $monthly->have_posts() ) : $monthly->the_post();
if( get_the_date('mY') != $ref_month ) {
if( $ref_month ) echo "\n".'</ul>';
echo "\n".'<h6>'.get_the_date('F').'</h6>';
echo "\n".'<ul>';
$ref_month = get_the_date('mY');
$counter = 0;
}
if( $counter++ < 5 ) echo "\n".' <li><a href='.get_permalink($post->ID).'>'.get_the_title($post->ID).'</a></li>';
endwhile;
echo "\n".'</ul>';
endif;
?>
Here's a more readable version of the solution provided by Michael
<?php
$posts = new WP_Query(array('posts_per_page' => -1));
if ($posts->have_posts()): ?>
<ul id="archives">
<?php
$prev_month = '';
while ($posts->have_posts()): $posts->the_post();
if (get_the_date('F Y') != $prev_month):
$prev_month = get_the_date('F Y'); ?>
<li class="month"><?= $prev_month; ?></li>
<?php endif; ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
本文标签: archivesDisplay all months with posts and inside each month show the 5 latest posts
版权声明:本文标题:archives - Display all months with posts and inside each month show the 5 latest posts 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744674313a2619017.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论