标题 | 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() ,这个是重置函数,不多做解释,可以用也可以不用。 |
随便看 |
|
在线学习网考试资料包含高考、自考、专升本考试、人事考试、公务员考试、大学生村官考试、特岗教师招聘考试、事业单位招聘考试、企业人才招聘、银行招聘、教师招聘、农村信用社招聘、各类资格证书考试等各类考试资料。