首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >wordpress调用指定ID分类下的最新、最多阅读、最后评论的内容

wordpress调用指定ID分类下的最新、最多阅读、最后评论的内容

作者头像
WordPress爱好者
发布2025-08-02 17:42:09
发布2025-08-02 17:42:09
12100
代码可运行
举报
运行总次数:0
代码可运行

在WordPress中,要调用指定分类ID下的:

最新文章

最多阅读(需要配合阅读量插件或自定义字段)

最新评论的文章

可以分别用WP_Query、get_posts、get_comments等实现。以下是无插件、可复用的代码片段,适合写在主题的functions.php或自定义插件中,并通过短代码或模板调用。

1.获取指定分类下的最新文章

代码语言:javascript
代码运行次数:0
运行
复制
function get_latest_posts_by_cat($cat_id = 1, $limit = 5) {
    $args = array(
        'cat'            => $cat_id,
        'posts_per_page' => $limit,
        'orderby'        => 'date',
        'order'          => 'DESC',
        'post_status'    => 'publish',
    );
    $query = new WP_Query($args);
    if ($query->have_posts()):
        echo '<ul>';
        while ($query->have_posts()): $query->the_post();
            echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
        endwhile;
        echo '</ul>';
        wp_reset_postdata();
    endif;
}

调用方法

代码语言:javascript
代码运行次数:0
运行
复制
<?php get_latest_posts_by_cat(3, 5); ?>

2.获取指定分类下最多阅读的文章

WordPress默认不记录阅读量,需配合插件(如PostViewsCounter)或自定义字段(如post_views_count)

假设你使用的是自定义字段post_views_count:

代码语言:javascript
代码运行次数:0
运行
复制
function get_most_viewed_posts_by_cat($cat_id = 1, $limit = 5) {
    $args = array(
        'cat'            => $cat_id,
        'posts_per_page' => $limit,
        'meta_key'       => 'post_views_count',
        'orderby'        => 'meta_value_num',
        'order'          => 'DESC',
        'post_status'    => 'publish',
    );
    $query = new WP_Query($args);
    if ($query->have_posts()):
        echo '<ul>';
        while ($query->have_posts()): $query->the_post();
            echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
        endwhile;
        echo '</ul>';
        wp_reset_postdata();
    endif;
}

调用方法

代码语言:javascript
代码运行次数:0
运行
复制
<?php get_most_viewed_posts_by_cat(3, 5); ?>

3.获取指定分类下最新评论的文章

获取最新评论,并按评论时间排序,返回对应文章列表。

代码语言:javascript
代码运行次数:0
运行
复制
function get_recent_commented_posts_by_cat($cat_id = 1, $limit = 5) {
    global $wpdb;

    $sql = "
        SELECT DISTINCT {$wpdb->posts}.ID, {$wpdb->posts}.post_title, {$wpdb->posts}.post_date, {$wpdb->comments}.comment_date
        FROM {$wpdb->posts}
        INNER JOIN {$wpdb->comments} ON {$wpdb->posts}.ID = {$wpdb->comments}.comment_post_ID
        WHERE {$wpdb->posts}.post_status = 'publish'
        AND {$wpdb->posts}.post_type = 'post'
        AND {$wpdb->posts}.ID IN (
            SELECT object_id FROM {$wpdb->term_relationships}
            WHERE term_taxonomy_id = %d
        )
        ORDER BY {$wpdb->comments}.comment_date DESC
        LIMIT %d
    ";

    $results = $wpdb->get_results($wpdb->prepare($sql, $cat_id, $limit));

    if ($results) {
        echo '<ul>';
        foreach ($results as $post) {
            echo '<li><a href="' . get_permalink($post->ID) . '">' . esc_html($post->post_title) . '</a></li>';
        }
        echo '</ul>';
    }
}

调用方法

代码语言:javascript
代码运行次数:0
运行
复制
<?php get_recent_commented_posts_by_cat(3, 5); ?>

调用内容

函数名

示例调用(分类 ID = 3)

最新文章

get_latest_posts_by_cat()

<?php get_latest_posts_by_cat(3, 5); ?>

最多阅读

get_most_viewed_posts_by_cat()

<?php get_most_viewed_posts_by_cat(3, 5); ?>

最新评论文章

get_recent_commented_posts_by_cat()

<?php get_recent_commented_posts_by_cat(3, 5); ?>

以上代码可以直接在模板文件中调用,也可以封装为短代码在Gutenberg区块编辑器中直接使用。

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
作者已关闭评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档