WordPress显示当前文章分类下的最新文章

代码2021-11-30538 人已阅来源:网络

今天分享大家一段可以快速生成wordpress文章页面,同分类栏目下面最新文章内容的代码,只要复制下面的代码到指定的位置就可以显示当前分类栏目下面的最新文章了,同时可以通过numberposts的数据来控制数量,通过orderby的值来设置排序的方式。

基本参数

'orderby' => 'date',                //按发布日期排序
'orderby' => 'modified',            //按修改时间排序
'orderby' => 'ID',              //按文章ID排序
'orderby' => 'comment_count',           //按评论最多排序
'orderby' => 'title',               //按标题排序
'orderby' => 'rand',                //随机排序
'order' => 'desc',           // 降序(递减,由大到小)

示例

<?php
    global $post;
    $categories = get_the_category(); //函数获取分类ID好
    foreach ($categories as $category){
?>
<ul>
    <?php
        $posts = get_posts('numberposts=80&orderby=rand&category='. $category->term_id);
        //通过get_posts函数,根据分类ID来获取这个ID下的文章内容。
        foreach($posts as $post){
    ?>
        <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
        <?php //显示出该分类下的文章标题,以及附加上超链接。 ?>
    <?php
        }
    ?>
</ul>
<?php
    }
?>