女武神的终末第三季免费观看,国产精品va无码二区,欧美日韩在线视频观看,国产在线观看网站

WordPress教程:相關(guān)文章中去除當(dāng)前文章ID方法

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

通常我們的WordPress都是調(diào)用全站的相關(guān)文章,當(dāng)然也會包括當(dāng)前文章,如何才能調(diào)用相關(guān)文章的時候去除當(dāng)前文章呢?方法并不難,在調(diào)用函數(shù)里添加一句 post__not_in 即可實現(xiàn),這個函數(shù)不要做太多解釋,按字面就可以理解了。

具體實施方法如下:

<?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), //去除當(dāng)前文章ID
‘showposts’ =>10,
‘caller_get_posts’=>1
);
$my_query = new WP_Query($args); ?>
<div>
<div>相關(guān)文章</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() ,這個是重置函數(shù),不多做解釋,可以用也可以不用。

0 0

企業(yè)建站推薦正版商業(yè)主題,國內(nèi)專業(yè)團隊開發(fā),完善售后,是您不二選擇。

正版主題商店

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

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

服務(wù)熱線

wordpress建站咨詢