当前位置:首页>WordPress>WordPress教程>不用插件快速实现WordPress相关文章调用方法

不用插件快速实现WordPress相关文章调用方法

我们很多网友会发现从WordPress官方平台下载的很多国外的主题没有页面相关文章调用列表,这个相对于SEO来说可能会有部分影响,至少让用户看不到同类的文章。那就需要我们自己添加这些功能,当然包括有一些插件可以实现,比如【Related Posts Thumbnails Plugin for WordPress 】插件是可以实现的,但是我们不要这么复杂的插件,这里我们就整理简单的代码实现。
//无插件实现WordPress相关文章 Edit by ciyol.cn
<div class="related_posts">
<h3>您可能也喜欢这几篇文章:</h3>
<ul>
<?php
$post_num = 10;
$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%20the_permalink();%20?>" title="<?php the_title(); ?>" target="_blank"><?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%20the_permalink();%20?>" title="<?php the_title(); ?>" target="_blank"><?php the_title(); ?></a></li>
<?php $i++;
} wp_reset_query();
}
if ( $i == 0 ) echo '<li>没有相关文章!</li>';
?>
</ul>
</div>

我们将代码添加到对应需要展示单页的页面位置。然后还需要自定义样式,例如CSS文件。
.related_posts {
margin-top: 5px;
padding-bottom: 10px;
border-bottom: 1px solid #ededed;
} 
.related_posts h3 {
margin-bottom: 5px;
}
.related_posts li {
margin-left: 20px;
color: #ccc;
list-style: square;
font-size: 14px;
line-height: 26px;
padding: 0 0 0 5px

}

然后根据我们主题实际的需要进行相关的调整。

©版权声明

本站资源大多来自网络,如有侵犯你的权益请联系管理员Ciyol 或给邮箱发送邮件 admin@ciyol.cn 我们会第一时间进行审核删除。站内资源为网友个人学习或测试研究使用,未经原版权作者许可,禁止用于任何商业途径!请在下载24小时内删除!


如果遇到评论下载的文章,评论后刷新页面点击对应的蓝字按钮即可跳转到下载页面本站资源少部分采用7z压缩,为防止有人压缩软件不支持7z格式,7z解压,建议下载7-zip(点击下载),zip、rar解压,建议下载WinRAR(点击下载)

给TA打赏
共{{data.count}}人
人已打赏
0 条回复A文章作者M管理员
    暂无讨论,说说你的看法吧
购物车
优惠劵
今日签到
有新私信 私信列表
搜索