WordPress类别文件循环打印

本文介绍了一种使用PHP编程语言来实现博客文章按类别展示的方法。通过遍历所有类别并获取每个类别下最新的几篇文章,实现了高效且美观的文章展示效果。具体包括获取每个分类下的最新一篇文章和随后的四篇额外文章。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


<?php
$categories = get_categories();

foreach ($categories as $cat) :?>
<div class="category_box">
<?php
// get most recent post in cat
query_posts('posts_per_page=1&cat='.$cat->cat_ID);
if (have_posts()) : while (have_posts()) : the_post();
?>
<h2><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?> </a></h2>
<?php the_content('continue reading...'); ?>
<?php
endwhile;
endif;
wp_reset_query();
// get 4 most recent posts in cat offset by 1
query_posts('posts_per_page=4&offset=1&cat='.$cat->cat_ID);
if(have_posts()) :?>
<ul class="more-stories">
<?php
while (have_posts()) :the_post();
?>
<li><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
<?php endwhile; ?>
</ul>
<?php endif; wp_reset_query();?>
</div><!--category_box-->
<?php endforeach; ?>

<?php $categories = get_categories();
foreach ($categories as $cat) : ?>
<?php // get most recent post in cat
query_posts('posts_per_page=5&cat='.$cat->cat_ID);
if (have_posts()) : while(have_posts()) : the_post();
?>
<h2>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
</h2>
<?php endwhile; ?>
<?php endif;
wp_reset_query();
?>
<?php endforeach; ?>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值