如何显示wordpress当前分类的文章列表?

2008年十一月4日 由 贝贝爸 Leave a reply »

wordpress单个文章页面的侧边栏如果还是显示和首页,分类页面一样的文章就没什么意思了。下面的代码是显示文章所属分类的文章列表,这样可以让访问者可以方便查看相关文章。回头再改改,看看能不能在分类页面上显示最受欢迎的文章。
添加下列代码在合时的位置即可

<?php
/*
single page?show current category articles
*/

?>
<?php
if ( is_single() ) :
global $post;
$categories = get_the_category();
foreach ($categories as $category) :
    
?>
    <li class="widget widget_recent_entries" id="
<?php $category->term_id;?>-posts">
        <h2 class="widgettitle">
<?php echo $category->name; ?></h2>
        <ul>
        
<?php
        
$posts = get_posts('numberposts=5&category='. $category->term_id);
        
foreach($posts as $post) :
        
?>
            <li>
            <a href="
<?php the_permalink(); ?>"><?php the_title(); ?></a>
            </li>
        
<?php endforeach; ?>
        </ul>
    </li>
<?php
endforeach; endif ; ?>
<?php
/*
end show current category articles
*/

?>

Popularity: 34%

相关文章

Advertisement

2 Responses

  1. 贝贝妈 说:

    看一下
    应该是用
    $orderby 的 ‘rand’ – Randomly sort results.
    改成类似这个样子你看看。
    $posts = get_posts(‘numberposts=5&orderby=rand&category=’. $category->term_id);
    参考http://codex.wordpress.org/Template_Tags/get_posts

  2. leaveoff 说:

    如何改为显示当前类别的随机文章啊?

Leave a Reply