WordPress教程:相關文章中去除當前文章ID方法
來源:易賢網(wǎng) 閱讀:906 次 日期:2014-09-02 15:26:58
溫馨提示:易賢網(wǎng)小編為您整理了“WordPress教程:相關文章中去除當前文章ID方法”,方便廣大網(wǎng)友查閱!

通常我們的wordpress都是調用全站的相關文章,當然也會包括當前文章,如何才能調用相關文章的時候去除當前文章呢?方法并不難,在調用函數(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), //去除當前文章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() ,這個是重置函數(shù),不多做解釋,可以用也可以不用。

更多信息請查看IT技術專欄

更多信息請查看CMS教程
易賢網(wǎng)手機網(wǎng)站地址:WordPress教程:相關文章中去除當前文章ID方法
由于各方面情況的不斷調整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復僅供參考,敬請考生以權威部門公布的正式信息和咨詢?yōu)闇剩?/div>
相關閱讀CMS教程

2025國考·省考課程試聽報名

  • 報班類型
  • 姓名
  • 手機號
  • 驗證碼
關于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機站點 | 投訴建議
工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務許可證:(云)人服證字(2023)第0102001523號
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關注公眾號:hfpxwx
咨詢QQ:1093837350(9:00—18:00)版權所有:易賢網(wǎng)