WordPress是如何实现相关文章的功能的?分享的几种方法(wordpress文章编辑器)

Wordpress1年前 (2023)发布 SUYEONE
720 0 0

WordPress实现相关文章功能有多种方式,包括使用插件和编写代码。虽然插件提供了一键配置的便利性,但可能会影响网站性能。因此,许多用户倾向于使用代码实现,尽管这需要一定的技术知识。下面我们将探讨几种通过代码实现相关文章的方法。

首先,无论采用哪种方法,输出的HTML结构基本相同,可以按照需求进行调整。以下是基本的HTML模板:

“`html

“`

**方法一:基于标签的相关文章**
这个方法通过获取当前文章的标签,然后随机选取一个标签,使用`query_posts()`函数查询该标签下的文章,以获取相关文章。

“`php
ID);
if ($post_tags) {
$tag_list = array();
foreach ($post_tags as $tag) {
$tag_list[] = $tag->term_id;
}
$post_tag = $tag_list[mt_rand(0, count($tag_list) – 1)];
$args = array(
‘tag__in’ => array($post_tag),
‘category__not_in’ => array(), // 排除的分类ID
‘post__not_in’ => array($post->ID),
‘posts_per_page’ => 6, // 显示相关文章的数量
‘caller_get_posts’ => 1
);
query_posts($args);
if (have_posts()) {
while (have_posts()) {
the_post();
update_post_cache($post);
// 输出文章标题和链接
}
} else {
echo ‘

  • 暂时没有相关文章
  • ‘;
    }
    }
    ?>
    “`

    **方法二:基于分类的相关文章**
    这种方法获取文章的分类ID,然后查询该分类下的其他文章作为相关文章。

    “`php
    ID);
    if ($cats) {
    $args = array(
    ‘category__in’ => array($cats[0]),
    ‘post__not_in’ => array($post->ID),
    ‘posts_per_page’ => 6,
    ‘caller_get_posts’ => 1
    );
    query_posts($args);
    if (have_posts()) {
    while (have_posts()) {
    the_post();
    update_post_cache($post);
    // 输出文章标题和链接
    }
    } else {
    echo ‘

  • 暂时没有相关文章
  • ‘;
    }
    }
    ?>
    “`

    **方法三:基于标签的SQL查询**
    使用WordPress数据库对象 `$wpdb` 直接执行SQL查询,获取相关文章。

    “`php
    ID);
    if ($post_tags) {
    $tag_list = ”;
    foreach ($post_tags as $tag) {
    $tag_list .= $tag->term_id . ‘,’;
    }
    $tag_list = substr($tag_list, 0, -1);
    $related_posts = $wpdb->get_results(”
    SELECT DISTINCT ID, post_title
    FROM {$wpdb->posts}, {$wpdb->term_relationships}, {$wpdb->term_taxonomy}
    WHERE {$wpdb->term_taxonomy}.term_taxonomy_id = {$wpdb->term_relationships}.term_taxonomy_id
    AND ID = object_id
    AND taxonomy = ‘post_tag’
    AND post_status = ‘publish’
    AND post_type = ‘post’
    AND term_id IN (‘$tag_list’)
    AND ID != ‘” . $post->ID . “‘
    ORDER BY RAND() LIMIT 6
    “);
    if ($related_posts) {
    foreach ($related_posts as $related_post) {
    // 输出文章标题和链接
    }
    } else {
    echo ‘

  • 暂时没有相关文章
  • ‘;
    }
    }
    ?>
    “`

    **方法四:基于分类的SQL查询**
    与方法三类似,但基于文章分类。

    “`php
    ID);
    if ($cats) {
    $related = $wpdb->get_results(”
    SELECT post_title, ID
    FROM {$wpdb->posts}, {$wpdb->term_relationships}, {$wpdb->term_taxonomy}
    WHERE {$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id
    AND {$wpdb->term_taxonomy}.taxonomy = ‘category’
    AND {$wpdb->term_taxonomy}.term_taxonomy_id = {$wpdb->term_relationships}.term_taxonomy_id
    AND {$wpdb->posts}.post_status = ‘publish’
    AND {$wpdb->posts}.post_type = ‘post’
    AND {$wpdb->term_taxonomy}.term_id = ‘” . $cats[0] . “‘
    AND {$wpdb->posts}.ID != ‘” . $post->ID . “‘
    ORDER BY RAND() LIMIT 6
    “);
    if ($related) {
    foreach ($related as $related_post) {
    // 输出文章标题和链接
    }
    } else {
    echo ‘

  • 暂时没有相关文章
  • ‘;
    }
    }
    ?>
    “`

    **方法五:基于作者的相关文章**
    获取当前文章的作者,然后查询该作者的其他文章。

    “`php
    $post_author,
    ‘post__not_in’ => array($post->ID),
    ‘posts_per_page’ => 6, // 显示相关文章的数量
    ‘orderby’ => ‘date’, // 按时间排序
    ‘caller_get_posts’ => 1
    );
    query_posts($args);
    if (have_posts()) {
    while (have_posts()) {
    the_post();
    update_post_cache($post);
    // 输出文章标题和链接
    }
    } else {
    echo ‘

  • 暂时没有相关文章
  • ‘;
    }
    ?>
    “`

    以上是实现相关文章功能的五种代码方法,你可以根据需要选择适合你的方法。每种方法的运行时间也列了出来,供参考。更多关于WordPress的教程和相关文章,请参阅其他资源。

    © 版权声明

    相关文章

    暂无评论

    暂无评论...
    ☺一键登录开启个人书签等功能!