好得很程序员自学网

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

wordpress显示随机文章实现方法 - WordPress

wordpress显示随机文章实现方法

首页随机显示文章 在wordpress里面并不难,也不需要安装复杂的插件,只需要在合适的php文件里面添加如下代码,这个完全归功于wordpress的模块化结构.

1.使用get_posts生成随机文章, 代码如下:

<?php  $rand_posts  = get_posts( 'numberposts=10&orderby=rand' );  foreach (  $rand_posts   as   $post  ) :  ?>  <li><a href= "<?php the_permalink(); ?>" ><?php the_title(); ?></a></li>  <?php  endforeach ; ?> 

2.使用随处可见的query_psots, 代码如下:

<?php  query_posts( 'showposts=10&orderby=rand' );  if  ( have_posts() ) :  while  ( have_posts() ) : the_post();  ?>  <li><em><?php  echo   $j ++;?></em><a href= "<?php the_permalink(); ?>" ><?php the_title(); ?></a></li>  <?php  endwhile ;  else :  ?> 

没有可显示的文章

<?php  endif ;  wp_reset_query();  ?> 

3.自定随机文章显示, 代码如下:

<ul>      <?php       $args  =  array (  'numberposts'  => 5,  'orderby'  =>  'rand'  );       $rand_posts  = get_posts(  $args  );       foreach (  $rand_posts   as   $post  ) : ?>          <li><a href= "<?php the_permalink(); ?>" ><?php the_title(); ?></a></li>      <?php  endforeach ; ?>      </ul> 

查看更多关于wordpress显示随机文章实现方法 - WordPress的详细内容...

  阅读:53次