首頁隨機顯示文章 在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>
新聞熱點
疑難解答
圖片精選