当前位置:首页>网站建设>WordPress文章底部添加相关文章

WordPress文章底部添加相关文章

释放双眼,带上耳机,听听看~!

本文的内容包括如何为WordPress的文章下面添加相关文章的功能,默认相关文章是同一个tag下的文章。非插件版~添加相关文章,不仅可以增加网站的浏览量,还可以减少页面的跳出率,鼓励用户去阅读更多的相关文章~添加相关文章后WordPress的效果如下:

在你的文章中插入相关代码

找到你的文章PHP文件,在你想插入的位置添加如下代码:

<div 	class="related-posts">
<h3>相关文章</h3>
<ul class="related_posts">
<?php
$post_num = 8;
$exclude_id = $post->ID;
$posttags = get_the_tags();
$i = 0;
if ($posttags) {
    $tags = '';
    foreach ($posttags as $tag) $tags.= $tag->term_id . ',';
    $args = array(
        'post_status' => 'publish',
        'tag__in' => explode(',', $tags) ,
        'post__not_in' => explode(',', $exclude_id) ,
        'caller_get_posts' => 1,
        'orderby' => 'comment_date',
        'posts_per_page' => $post_num,
    );
    query_posts($args);
    while (have_posts()) {
        the_post(); ?>
<li><a rel="bookmark" href="<?php
        the_permalink(); ?>" title="<?php
        the_title(); ?>" target="_blank" style="color: #00ccff;"><?php
        the_title(); ?></a></li>
<?php
        $exclude_id.= ',' . $post->ID;
        $i++;
    }
    wp_reset_query();
}
if ($i < $post_num) {
    $cats = '';
    foreach (get_the_category() as $cat) $cats.= $cat->cat_ID . ',';
    $args = array(
        'category__in' => explode(',', $cats) ,
        'post__not_in' => explode(',', $exclude_id) ,
        'caller_get_posts' => 1,
        'orderby' => 'comment_date',
        'posts_per_page' => $post_num - $i
    );
    query_posts($args);
    while (have_posts()) {
        the_post(); ?>
<li><a rel="bookmark" href="<?php
        the_permalink(); ?>"  title="<?php
        the_title(); ?>" target="_blank" style="color: #00ccff;"><?php
        the_title(); ?></a></li>
<?php
        $i++;
    }
    wp_reset_query();
}
if ($i == 0) echo '<li>没有相关文章!</li>';
?>
</ul>
</div>

修改样式

添加完上面的代码后,WordPress就会自动在每一篇文章后面添加相关文章模块,再根据自己的需求,在style.css添加你的样式就可以了~

欢迎访问秀主题博客,分享简单实用WP教程
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

给TA打赏
共{{data.count}}人
人已打赏
网站建设

WordPress从服务器搬到本地(换域名的效果)

2024-6-20 9:49:20

网站建设

WordPress用户权限角色级别完整说明

2024-6-23 7:48:05

温馨提示:

1.本站大部分内容均收集于网络!若内容若侵犯到您的权益,请发送邮件至:971318156@qq.com,我们将第一时间处理!

2.资源所需价格并非资源售卖价格,是收集、整理、编辑详情以及本站运营的适当补贴,并且本站不提供任何免费技术支持。

3.所有资源仅限于参考和学习,版权归原作者所有,更多请阅读网站声明

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索