
可以自动将Wordpress站点的文章按分类显示在一个页面中,用作CMS主题布局很实用。
用下代码替换首页模板主循环,则按分类显示各分类最新的5篇文章。
<?php
global $cat;
$cats = get_categories(array(
\'child_of\' => $cat,
\'parent\' => $cat,
\'hide_empty\' => 0
));
$c = get_category($cat);
foreach($cats as $the_cat){
$posts = get_posts(array(
\'category\' => $the_cat->cat_ID,
\'numberposts\' => 5,
));
if(!empty($posts)){
echo \'
<div class=\"item cat_item\">
<div class=\"item_title\"><h2><a title=\"\'.$the_cat->name.\'\" href=\"\'.get_category_link($the_cat).\'\">\'.$the_cat->name.\'</a></h2></div>
<ul class=\"box_list\">\';
foreach($posts as $post){
echo \'<li><span class=\"alignright\">\'.mysql2date(\'Y-m-d\', $post->post_date).\'</span>
<a title=\"\'.$post->post_title.\'\" href=\"\'.get_permalink($post->ID).\'\">\'.$post->post_title.\'</a></li>\';
}
echo \'</ul>
</div>\';
}
}
?>用下代码替换分类归档模板主循环,则显示该分类下所有子分类的最新5篇文章,并按分类显示。如果当前分类下不存在子分类,则显示该分类的文章列表!
<?php
global $cat;
$cats = get_categories(array(
\'child_of\' => $cat,
\'parent\' => $cat,
\'hide_empty\' => 0
));
$c = get_category($cat);
if(empty($cats)){
?>
<div class=\"item\">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class=\"post\">
<h2><a title=\"<?php the_title(); ?>\" href=\"<?php the_permalink(); ?>\"><?php the_title(); ?></a></h2>
<p><?php the_excerpt(); ?></p>
<p><a href=\"<?php the_permalink(); ?>\">全文阅读>></a></p>
<div class=\"meta\"><?php the_time(\'Y-m-d\'); ?> | 标签: <?php the_tags(\'\', \' , \', \'\'); ?></div>
</div>
<?php endwhile; ?>
<?php else: ?>
<div class=\"post\"><p>文章稍后更新</p></div>
<?php endif; ?>
</div>
<div class=\"navigation\">
<span class=\"alignleft\"><?php next_posts_link(\'« Older posts\') ?></span>
<span class=\"alignright\"><?php previous_posts_link(\'Newer posts »\') ?></span>
</div>
<?php
}else{
foreach($cats as $the_cat){
$posts = get_posts(array(
\'category\' => $the_cat->cat_ID,
\'numberposts\' => 10,
));
if(!empty($posts)){
echo \'
<div class=\"item cat_item\">
<div class=\"item_title\"><h2><a title=\"\'.$the_cat->name.\'\" href=\"\'.get_category_link($the_cat).\'\">\'.$the_cat->name.\'</a></h2></div>
<ul class=\"box_list\">\';
foreach($posts as $post){
echo \'<li><span class=\"alignright\">\'.mysql2date(\'Y-m-d\', $post->post_date).\'</span>
<a title=\"\'.$post->post_title.\'\" href=\"\'.get_permalink($post->ID).\'\">\'.$post->post_title.\'</a></li>\';
}
echo \'</ul>
</div>\';
}
}
}
?>本站除原创内容,其余内容均收集自互联网,仅限用于学习和研究目的,本站不对其内容的合法性、可用性承担任何责任。本站禁止以任何形式发布或转载任何违法相关信息,若您发现请立即向站长举报;用户投稿一切资源内容不代表本站立场,并不代表本站赞同其观点和对其真实性负责。
如有版权内容,其版权均归原作者所有,本站虽力求保存原有版权信息,但因众多资源经多次转载,已无法确定其真实来源,故敬请原作者谅解!为尊重作者劳动成果,请购买正版支持作者,谢谢!若您对「HULIKU.COM」所载资源作品版权归属存有异议,请发送邮箱:ihuliku@qq.com 进行处理。
本站资源文件大多存储至云盘,如您发现图片或链接失效,请联系站长或作者,我们将及时进行更新。资源都是经过站长或作者收集测试优化后进行发布分享。如若转载请在贵站文内以超链形式注明狐狸库文章出处,谢谢合作!













![WordPress子比主题美化教程合集[2023/04/17]-狐狸库](https://huliku.com/pic/2023032607024935.png)
![狐狸库自定义子比主题底部footer页脚美化(自适应)[06.26]更新-狐狸库](https://huliku.com/pic/2023062610013835.png)























请登录后查看评论内容