久青草国产观看在线视频,在线观看欧美日女,777毛片,亚洲国产精品99久久久久久久

WordPress教程:相關文章中去除當前文章ID方法

2018-08-18 wordpress經驗
  • 文章介紹
  • 快速入門
  • 評價&建議

通常我們的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() ,這個是重置函數,不多做解釋,可以用也可以不用。

0 0

企業建站推薦正版商業主題,國內專業團隊開發,完善售后,是您不二選擇。

正版主題商店

主題貓WP建站,累計幫助1300+客戶成功建站,為站長提供支持!

立刻開啟你的建站之旅
QQ在線客服

服務熱線

wordpress建站咨詢