My Computer · 2024/06/17 0

非插件纯代码实现wordpress相关文章

一直在用WPJAM Basic的相关文章插件,但是最近不知道为啥会重复出现四次相关文章,测试了几款其他插件感觉并不理想,老规矩,还是请ChatGPT大哥出马

你精通Wordpress和wordpress插件的编写。我需要你写一个wordpress相关文章的代码,该代码直接在模板函数文件中使用,不要以插件方式,要求如下:
自动在文章末尾添加相关文章列表;
在相关文章中排除当前文章;
相关文章数量设置为6;
先根据文章标签查找相关文章,新发表的文章排在前边;
如果文章标签的相关文章不足设置的数量,则从当前文章所在分类取最新的文章,以补足数量;
如果文章没有设置标签,则随机显示该文章所在分类的对应数量的文章。

请给出完整代码

// 将相关文章添加到内容中
function add_related_posts_to_content($content) {
    // 判断是否是单篇文章、在主循环中、以及主查询
    if (is_single() && in_the_loop() && is_main_query()) {
        global $post;
        // 获取相关文章的HTML
        $related_posts_html = get_related_posts($post->ID);
        // 将相关文章的HTML添加到内容末尾
        $content .= $related_posts_html;
    }
    return $content;
}
// 为内容过滤器添加自定义函数
add_filter('the_content', 'add_related_posts_to_content');

// 获取相关文章
function get_related_posts($post_id) {
    $max_related_posts = 6; // 最多相关文章数
    $related_posts = []; // 相关文章数组

    // 获取当前文章的标签和分类
    $post_tags = get_the_tags($post_id);
    $post_categories = get_the_category($post_id);

    // 如果有标签,通过标签获取相关文章
    if ($post_tags) {
        $tag_ids = wp_list_pluck($post_tags, 'term_id');
        $tag_args = [
            'tag__in' => $tag_ids,
            'post__not_in' => [$post_id],
            'posts_per_page' => $max_related_posts,
            'orderby' => 'date',
            'order' => 'DESC',
        ];
        $related_posts = get_posts($tag_args);
    }

    // 如果相关文章数少于最大值,通过分类获取更多文章
    if (count($related_posts) < $max_related_posts && $post_categories) {
        $cat_ids = wp_list_pluck($post_categories, 'term_id');
        $cat_args = [
            'category__in' => $cat_ids,
            'post__not_in' => [$post_id],
            'posts_per_page' => $max_related_posts - count($related_posts),
            'orderby' => 'date',
            'order' => 'DESC',
        ];
        $more_related_posts = get_posts($cat_args);
        $related_posts = array_merge($related_posts, $more_related_posts);
    }

    // 如果相关文章仍然不足,通过分类随机获取更多文章
    if (count($related_posts) < $max_related_posts && $post_categories) {
        $cat_ids = wp_list_pluck($post_categories, 'term_id');
        $random_cat_args = [
            'category__in' => $cat_ids,
            'post__not_in' => [$post_id],
            'posts_per_page' => $max_related_posts - count($related_posts),
            'orderby' => 'rand',
        ];
        $random_related_posts = get_posts($random_cat_args);
        $related_posts = array_merge($related_posts, $random_related_posts);
    }

    // 生成相关文章列表的HTML
    if (!empty($related_posts)) {
        $related_posts_html = '<div class="related-posts"><h4>相关文章</h4>';
        foreach ($related_posts as $related_post) {
            $related_posts_html .= '<div style="margin-bottom: 5px; position: relative;">';
            $related_posts_html .= '<span style="position: absolute; font-size: 1.2em; margin-right: 5px;">•</span>';
            $related_posts_html .= '<a href="' . get_permalink($related_post->ID) . '" style="padding-left: 15px; display: inline-block; position: relative; top: 0; left: 10px;">' . get_the_title($related_post->ID) . '</a>';
            $related_posts_html .= '</div>';
        }
        $related_posts_html .= '</div>';
        return $related_posts_html;
    }

    return '';
}

效果如下: