无插件实现WordPress相关文章调用方法

 杭州seo优化   2014-12-06 15:52   222 views 人阅读  0 条评论

在前面老左分享到"放弃使用WordPress Related Posts插件的理由",本来还想用另外一款"Yet Another Related Posts Plugin"插件的,国外用户比较多,而且非常的热门,Matt Cutts也在博文中把这款相关文章插件作为推荐的首选。但是我在使用后发现其他都还不错,就是唯一不能自定义位置,他默认直接在文章底部显示,万一文章有模板底部添加内容,那内容就在调用下面。所以我还是放弃使用插件,直接采用无插件实现WordPress相关文章调用方法。

第一步,在需要添加相关内容调用的位置,编辑下面代码:

<div class="post-relate">
<h3>你可能喜欢如下内容:</h3>
<ul>
<?php
$post_tags = wp_get_post_tags($post->ID);
if ($post_tags) {
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(NULL),
'post__not_in' => array($post->ID),
'showposts' => 6,
'caller_get_posts' => 1
);
query_posts($args);
if (have_posts()) : while (have_posts()) : the_post(); update_post_caches($posts); ?>
<li>
<a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
</li>
<?php endwhile; else : ?>
<?php $ashu_cats = wp_get_post_categories($post->ID);
if( $ashu_cats ){
$args = array(
'category__in' => array( $ashu_cats[0] ),
'post__not_in' => array( $post->ID ),
'showposts' => 6,
'caller_get_posts' => 1
);
query_posts($args);
if( have_posts()):while(have_posts()):the_post();update_post_caches($posts);?>
<li>
<a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
</li>
<?php endwhile; endif; wp_reset_query(); } ?>
<?php endif; wp_reset_query(); } ?>
</ul>
<div class="clear"></div>
</div>

第二步:样式添加

如果添加上述的代码后感觉界面还不错,就直接使用,如果不行,需要用到下面的样式。

/* post-relate */
.post-relate{padding:10px 5px;color:#888;}
.post-relate h3{margin:0;line-height:22px;Verdana;font-size:14px;}
.post-relate ul{margin:5px 15px;padding:0px 5px;}
.post-relate li{margin:0px 0px;padding:2px 0px 2px;}

样式还需要根据自己的需要调整。这样我们不用插件也能实现效果,降低WP的内存消耗。能不用插件尽量不使用。

写在结尾:以上就是无插件实现WordPress相关文章调用方法的详细内容,更多请关注【杭州SEO博客】其它相关文章!
本文地址:http://seo.huashi123.cn/4846.html
版权声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系我们。

 相关文章  关键词:

 发表评论


表情