简要咨询咨询QQ网站导航网站搜索手机站点联系我们设为首页加入收藏 

WordPress教程:相关文章中去除当前文章ID方法

来源:易贤网   阅读:1038 次  日期:2014-09-02 15:26:58

温馨提示:易贤网小编为您整理了“WordPress教程:相关文章中去除当前文章ID方法”,方便广大网友查阅!

通常我们的wordpress都是调用全站的相关文章,当然也会包括当前文章,如何才能调用相关文章的时候去除当前文章呢?方法并不难,在调用函数里添加一句 post__not_in 即可实现,这个函数不要做太多解释,按字面就可以理解了。

具体实施方法如下:

<?php

$backup = $post;

$tags = wp_get_post_tags($post->ID);

$tagIDs = array();

if ($tags) {

$tagcount = count($tags);

for ($i = 0; $i < $tagcount; $i++) {

$tagIDs[$i] = $tags[$i]->term_id;

}

$args=array(

‘tag__in’ => $tagIDs,

‘post__not_in’ => array($post->ID), //去除当前文章ID

‘showposts’ =>10,

‘caller_get_posts’=>1

);

$my_query = new WP_Query($args); ?>

<div>

<div>相关文章</div>

<ul>

<?php if( $my_query->have_posts() ) {

while ($my_query->have_posts()) : $my_query->the_post(); ?>

<li><a href=”<?php the_permalink() ?>” rel=”bookmark” target=”_self” title=”<?php the_title(); ?>”><?php the_title(); ?></a></li>

<?php endwhile;

} else { ?>

<?php $recent=new WP_Query( “showposts=10&caller_get_posts=1&orderby=rand”); while($recent->have_posts()) : $recent->the_post();?>

<li><a href=”<?php the_permalink() ?>” rel=”bookmark” target=”_self” title=”<?php the_title(); ?>”><?php the_title(); ?></a></li>

<?php endwhile;    ?>

<?php } ?>

</ul>

</div>

<?php } else { ?>

<div>

<div>随机文章</div>

<ul>

<?php $recent=new WP_Query( “showposts=10&caller_get_posts=1&orderby=rand”); while($recent->have_posts()) : $recent->the_post();?>

<li><a href=”<?php the_permalink() ?>” rel=”bookmark” target=”_self” title=”<?php the_title(); ?>”><?php the_title(); ?></a></li>

<?php endwhile;    ?>

</ul>

</div>

<?php } ?>

<?php $post = $backup; wp_reset_query(); ?>

这里我还用到了 wp_reset_query() ,这个是重置函数,不多做解释,可以用也可以不用。

更多信息请查看IT技术专栏

更多信息请查看CMS教程
点此处就本文及相关问题在本站进行非正式的简要咨询(便捷快速)】     【点此处查询各地各类考试咨询QQ号码及交流群
上一篇:迁移与合并wordpress网站的过程与心得
下一篇:WordPress在文章中显示广告(非上下、左右角)
易贤网手机网站地址:WordPress教程:相关文章中去除当前文章ID方法
由于各方面情况的不断调整与变化,易贤网提供的所有考试信息和咨询回复仅供参考,敬请考生以权威部门公布的正式信息和咨询为准!
相关阅读      CMS教程