用下代碼替換首頁模板主循環,則按分類顯示各分類最新的 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 >
<div ><h2><a
title="'.$the_cat->name.'"
href="'.get_category_link($the_cat).'">'.$the_cat->name.'</a></h2></div>
<ul >';
foreach($posts as $post){
echo '<li><span >'.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 >
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div >
<h2><a title="<?php the_title(); ?>"
href="<?php%20the_permalink();%20?>"><?php the_title();
?></a></h2>
<p><?php the_excerpt(); ?></p>
<p><a href="<?php%20the_permalink();%20?>"> 全文閲讀>></a></p>
<div ><?php the_time('Y-m-d'); ?> | 標籤: <?php the_tags('', ' , ', ''); ?></div>
</div>
<?php endwhile; ?>
<?php else: ?>
<div ><p> 文章稍後更新</p></div>
<?php endif; ?>
</div>
<div >
<span ><?php next_posts_link('« Older posts') ?></span>
<span ><?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 >
<div ><h2><a
title="'.$the_cat->name.'"
href="'.get_category_link($the_cat).'">'.$the_cat->name.'</a></h2></div>
<ul >';
foreach($posts as $post){
echo '<li><span >'.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>';
}
}
}
?>