- WordPress 代码实现相关文章(列表模式)功能WordPress代码实现相关文章这个代码所实现的相关文章的相关度非常高(优先通过 Tags 标签相关,之后是同分类下文章,排除当前文章. <h3>Related Posts</h3> <ul> <?php $post_num = 5; // ...
- WordPress添加相关文章功能(含缩略图相关文章)网上为wordpress调取相关文章的代码很多,本站也曾经分享过几款调取相关文章的代码。今天再收集了一款。相关文章的获取思路是:Tags标签相关>同分类下文章,也就是说,先获取标签相同的文章,如果还达不到数量...
- 为WordPress页面(page)添加相关页面方法wordpress的相关文章功能用的比较多,不过有时候出于需要,想给wordpress的页面(page)也添加一个相关页面的功能。下面就给大家分享一下如何给页面也增加相关文章的功能。 一般文章(post)都是通过 标签 或 分...
- wordpress相关文章最新代码分享先说说实现wordpress相关文章的思路,今天给大家分享的wordpress相关文章是基于相关性:标签(tag)相关+分类(category)相关来实现的,最后如果数量不够,在补充几篇随机日志。是wordpress相关文章代码强力版的...
- 免插件实现仿无觅相关文章图文模式功能(增强版)免插件实现仿无觅相关文章图文模式功能,没啥多说的,代码来自devework。本方法实现的相关文章原理是通过获取该文章tag(标签),如果相关tag不足,再找到同一分类的文章,这些文章即为“相关文章”。而图片的话,...
- wordpress侧边栏随页面滚动完美版其实关于wordpress侧边栏随页面滚动的代码,本站已经收集了几篇,但是以前的代码都不支持侧边栏滚动时到一定位置停止(有时候我们不希望侧边栏出现无限滚动的情况,所以才会有滑动到一定位置时希望它停止的要求)...
- 缓存WordPress的Sidebar(侧边栏)的技巧缓存WordPress的Sidebar(侧边栏)的技巧。缓存WordPress的相关栏目无疑会减轻服务器的压力,所以这里就给大家分享一种缓存WordPress的Sidebar(侧边栏)的技巧。 对 Sidebar 缓存的代码分享下,几点说明: 1. 可以自...
- WordPress获取某一页面的文章内容的代码WordPress获取某一页面的文章内容的代码。使用wordpress有时候可能需要在首页或其它页面调用某一页面的内容或者其它相关信息,而具体方法我们是可以通过要获取的页面的ID从而调用该页面的内容。下面是具体相关的...
wordpress免插件实现相关文章代码
给wordpress网站增加相关文章功能可以在一定程度上提高网站的pv,不过很多朋友可能出于懒得折腾的原因吧,都还是习惯采用wordpress相关文章插件,但是方便的同时也许牺牲了网站的速度,毕竟插件要考虑很多主题的兼容性,可能会增加很多对你网站无用的代码。所以个人的做法是能用代码达到效果就绝对不用插件,玩wordpress,玩的就是折腾嘛!再说在折腾的过程还可以学到点东西呢,何乐而不为?下面就给大家分享一下wordpress免插件实现相关文章代码。
<h3>Related Posts</h3>
<ul>
<?php
$post_num = 5; // 數量設定.
$exclude_id = $post->ID; // 單獨使用要開此行 //zww: edit
$posttags = get_the_tags(); $i = 0;
if ( $posttags ) {
$tags = ''; foreach ( $posttags as $tag ) $tags .= $tag->term_id . ','; //zww: edit
$args = array(
'post_status' => 'publish',
'tag__in' => explode(',', $tags), // 只選 tags 的文章. //zww: edit
'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(); ?>"><?php the_title(); ?></a></li>
<?php
$exclude_id .= ',' . $post->ID; $i ++;
} wp_reset_query();
}
if ( $i < $post_num ) { // 當 tags 文章數量不足, 再取 category 補足.
$cats = ''; foreach ( get_the_category() as $cat ) $cats .= $cat->cat_ID . ',';
$args = array(
'category__in' => explode(',', $cats), // 只選 category 的文章.
'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(); ?>"><?php the_title(); ?></a></li>
<?php $i++;
} wp_reset_query();
}
if ( $i == 0 ) echo '<li>没有相关文章!</li>';
?>
</ul>
把上述代码复制到你主题模板需要显示相关文章的位置即可(一般是插入在single.php文件中),该段代码可以通过调取tags来实现相关文章功能(所以我们有必要在发布文章的时候为文章增加tag),并且默认设置为5篇相关文章(可以修改代码中的文章数量显示),当相关文章数量不够时采用category补足。
上面的相关文章代码由于采用tag作为相关文章来源所以有时侯相关度就显得不是很高,如果喜欢用插件来实现相关日志的功能,那么推荐你使用yet-another-related-posts-plugin。相对来说yet-another-related-posts-plugin的相关文章无论从设置上面还是相关度都是相对不错的。
来源:http://zww.me/archives/25353
改进版,相关文章数量不够时再增加随机文章来补齐。
<h3>Related Posts</h3>
<ul>
<?php
$post_num = 5;
$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(); ?>"><?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" title="<?php the_title(); ?>" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php
$exclude_id .= ',' . $post->ID; $i ++;
} wp_reset_query();
}
if ( $i < $post_num ) {
$args = array(
'post__not_in' => explode(',', $exclude_id),
'caller_get_posts' => 1,
'orderby' => 'rand',
'posts_per_page' => $post_num - $i
);
query_posts($args);
while( have_posts() ) { the_post(); ?>
<li><a rel="bookmark" title="<?php the_title(); ?>" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php
$exclude_id .= ',' . $post->ID; $i ++;
} wp_reset_query();
}
if ( $i == 0 ) echo '<li>No Related Articles!</li>';
?>
</ul>