方法一:代码最简单的方法 在需要显示的地方直接调用如下代码(张自然现在就用这种方法,经张自然修改后,兼容inove系列主题 - <li class="widget widget_numberposts">2 Z# `9 l3 J9 I9 i8 F
- <h3>随遍看看吧</h3>9 [ O8 Y, I! @4 p# M/ G6 @2 P
- <ul>% B( `) ~* [3 z2 l( M8 |1 ?
- <?php $rand_posts = get_posts('numberposts=8&orderby=rand');% ^ t) g6 A+ ]9 d' `
- foreach( $rand_posts as $post ) : ?>
) z; u8 P1 v2 f& P+ n - <li>
' w2 ^! F6 N, V0 _1 t* m: j - <a target="_blank" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>, ]3 a" W! v4 r/ g8 x
- </li>
5 v7 H0 j2 H* Z1 M1 W - <?php endforeach; ?>0 @, g9 P2 ]6 g4 L. f; o
- </ul>
复制代码这个方法虽然简单,但用到了get_posts,如果将代码放在子页模板里,在他之后的代码,比如如果在后面同时调用了当前文章的评论,那评论内容很可能,出现的是最后一个随机到的文章的评论,而非当前文章的评论。
5 B! N; ?% g2 J方法二:在随机文章中显示标题和文章摘要在需要显示的地方直接调用如下代码/ r+ d6 s: }8 Y- Z
- <?php2 ^* ]: Z R; c+ J; m
- query_posts(array(‘orderby’ => ‘rand’, ‘showposts’ => 1));1 W5 o6 S5 W# v2 b; [
- if (have_posts()) :
- j3 o* V: `; R- m. E - while (have_posts()) : the_post();
7 B( m0 y/ n. Z$ o9 `1 J3 H5 U9 N# o/ ? - the_title(); //这行去掉就不显示标题. e. e) V9 w% \6 }4 o4 z& G
- the_excerpt(); //去掉这个就不显示摘要了
0 t' O* `+ F# M( ` - endwhile;
6 j; B/ H" N% s8 x d - endif; ?>
复制代码 方法三:用query_posts生成随机文章列表在需要显示的地方直接调用如下代码 - <?php
$ V* }/ ^; P) U2 E% j - query_posts(array(‘orderby’ => ‘rand’, ‘showposts’ => 2));
2 _4 B7 o- ^6 {$ d7 Q& D' e5 U - if (have_posts()) :
! t& ?" ~# }- c9 w - while (have_posts()) : the_post();?>* ]4 _# T9 s' W
- <a href=”<?php the_permalink() ?>”* v; ]3 V6 g8 q' Y+ l
- rel=”bookmark”# W4 z R% P7 H9 `# L: H* d" v0 u
- title=<?php the_title(); ?>”><?php the_title(); ?></a> * B' ?/ d, ^9 _4 z7 E
- <?php comments_number(”, ‘(1)’, ‘(%)’); ?>
, O5 P7 H9 F! ~4 _" N - <?php endwhile;endif; ?>
复制代码 方法四:在function.php中加入如下代码在function.php中加入如下代码 - /**
: p: j9 p4 Y1 q) h' d, X% @4 K! ` - * 随机文章( S2 u( Y7 I8 T0 F q3 ^/ v
- */9 f) h/ M! Y4 z- p
- function random_posts($posts_num=5,$before='<li>',$after='</li>'){8 Q- H5 z( H2 r' @# S
- global $wpdb;
& W# I2 @1 I# r - $sql = "SELECT ID, post_title,guid
* _/ V M# h1 c7 h& z! g0 h - FROM $wpdb->posts8 d2 Q7 ]; f6 }3 s
- WHERE post_status = 'publish' ";1 }2 V: L7 r% j' I6 g" H, O
- $sql .= "AND post_title != '' ";. s0 G5 O2 A X
- $sql .= "AND post_password ='' ";
, y' S! Q5 q+ X o4 ^$ J - $sql .= "AND post_type = 'post' ";5 e# @) B4 T( N: e j8 `- u
- $sql .= "ORDER BY RAND() LIMIT 0 , $posts_num ";
: x: |3 C8 ^' c. p5 k7 m - $randposts = $wpdb->get_results($sql);
, z: {3 H: J* F2 D# { - $output = '';9 T1 o1 E! |' b5 I( M. {$ C
- foreach ($randposts as $randpost) {
0 ^, w6 R/ K! {" A+ L0 b e - $post_title = stripslashes($randpost->post_title);
% I x8 a: d4 n; \4 Q4 z( q I2 E - $permalink = get_permalink($randpost->ID);( y8 Q$ t- y: q4 U* f, i
- $output .= $before.'<a href="'
0 [5 ]1 d: P7 y! F9 B - . $permalink . '" rel="bookmark" title="';1 G, T1 \5 R5 M9 X0 M& u# r& ?$ ^ |
- $output .= $post_title . '">' . $post_title . '</a>';9 j) Q7 O4 J; ]; s# n/ A. B
- $output .= $after;7 Y7 s! g b# p+ T' s/ z
- }
! \( B: _% k( Z) t - echo $output;# b* i+ B( R; J9 k
- }
4 q$ k9 `7 e7 o1 I: K - 在需要显示的地方调用如下代码
6 H, F& b! i1 @1 L( f - <div>1 p7 V3 [0 }/ d
- <h3>随便找点看看!</h3>3 @( D; \6 U1 T# z. r
- <ul>9 x8 z/ W. ]' T
- <?php random_posts(); ?>
+ j/ Y- @. X" G6 @, L8 z; j% p% }7 {3 o - </ul>
& ~9 a3 S7 o; A& t8 l0 n - </div><!-- 随机文章 -->
复制代码
, `& b$ m5 z5 W, b6 n2 a2 P- {3 ?% `1 e X
7 P$ P% {7 U% H2 _1 B: \
|