好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

wordpress调用随机文章的一些例子 - WordPress

wordpress调用随机文章的一些例子

随机文章如果单条sql是非常的简单直接使用rand就可以得到了,但是在wordpress中我们需要稍加处理即可了,下面我总结了一些方法,希望对各位有帮助.

调用随机文章代码:

<?php  $rand_posts  = get_posts(‘numberposts=10&orderby=rand’);  foreach (  $rand_posts   as   $post  ) :  ?>  <!–下面是你想自定义的Loop–>  <li><a href=]<?php the_permalink(); ?>]><?php the_title(); ?></a></li>  <?php  endforeach ; ?> 

调用相关文章代码, 在文章页显示相关文章,代码如下:

<?php  $tags  = wp_get_post_tags( $post ->ID);  if  ( $tags ) {  $first_tag  =  $tags [0]->term_id;  $args = array (‘  tag__in’ =>  array ( $first_tag ),  ‘post__not_in’ =>  array ( $post ->ID),  ‘showposts’=>10,  ‘caller_get_posts’=>1  );  $my_query  =  new  WP_Query( $args );  if (  $my_query ->have_posts() ) {  while  ( $my_query ->have_posts()) :  $my_query ->the_post(); ?>  <li><a href=]<?php the_permalink() ?>] rel=]bookmark] title=]<?php the_title_attribute(); ?>]><?php the_title();?> <?php comments_number(‘ ‘,’(1)’,’(%)’); ?></a></li>  <?php  endwhile ;  }  }  wp_reset_query();  ?> 

调用同分类随机文章, 将下面代码放到主题文章页面single模板或者边栏sidebar模板适当位置即可:

<ul>      <?php      $cat  = get_the_category();      foreach ( $cat   as   $key => $category ){      $catid  =  $category ->term_id;     }      $args  =  array ( 'orderby'  =>  'rand' , 'showposts'  => 8, 'cat'  =>  $catid  );      $query_posts  =  new  WP_Query();      $query_posts ->query( $args );       while  ( $query_posts ->have_posts()) :  $query_posts ->the_post();      ?>      <li><a href= "<?php the_permalink(); ?>" ><?php the_title(); ?></a></li>      <?php  endwhile ;?>  </ul> 

查看更多关于wordpress调用随机文章的一些例子 - WordPress的详细内容...

  阅读:48次