wordpress调用相关文章不用插件
转自 好啦制造 正文如下:
我相信,在设计wordpress主题的时候,很多人都会在主题中加入相关文章这一个功能。但,在很多时候,我们都会借用Simple tags或其他插件的函数来完成这更能,但这就造成了一个问题,那就是你所涉及的wordpress主题对于插件的依赖性过高,因此,如何结束这个问题呢,并不是没有办法,现在老高告诉你吧,方法很简单,在你需要加入相关文档的地方加入如下代码就可以了:
<?php
//这是一个循环,现在列出的是5片文档,通过相关tag来实现
$tags = wp_get_post_tags($post->ID);
if ($tags) {
echo ‘相关文章’;
$first_tag = $tags[0]->term_id;
$args=array(
‘tag__in’ => array($first_tag),
‘post__not_in’ => array($post->ID),
’showposts’=>5,
‘caller_get_posts’=>1
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><a href=”<?php the_permalink() ?>” rel=”bookmark” title=”Permanent Link to <?php the_title_attribute(); ?>”><?php the_title(); ?></a></p>
<?php
endwhile;
}
}
?>在你需要的地方加入这个代码就可以了,注意使用方法,其中的
echo’相关文章’可以去掉,如果你自己加标题的话,
showposts这个参数为设置显示文章的数量
你也可以把
<p><a href=”<?php the_permalink() ?>” rel=”bookmark” title=”Permanent Link to <?php the_title_attribute(); ?>”><?php the_title(); ?></a></p> 更换为
<li><a href=”<?php the_permalink() ?>” rel=”bookmark” title=”Permanent Link to <?php the_title_attribute(); ?>”><?php the_title(); ?></a></li>
转发到新浪微博

