|
方法一:代码最简单的方法 在需要显示的地方直接调用如下代码(张自然现在就用这种方法,经张自然修改后,兼容inove系列主题 - <li class="widget widget_numberposts">$ R p1 W7 ?; S# _2 [, Z8 q4 Y
- <h3>随遍看看吧</h3>
5 z5 D$ _) E! o0 e3 h - <ul>
9 {. d* d( h6 C - <?php $rand_posts = get_posts('numberposts=8&orderby=rand');( }4 R/ C. g0 M: V9 R! P& e: c
- foreach( $rand_posts as $post ) : ?>1 d8 k f% m6 j8 _' J
- <li>8 V! N7 M; E# H4 ^$ G
- <a target="_blank" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>2 {8 O7 A# {; q, C/ X
- </li># D- t* [! Z/ E! u
- <?php endforeach; ?>
, X; N/ `# I' o/ {5 v - </ul>
复制代码这个方法虽然简单,但用到了get_posts,如果将代码放在子页模板里,在他之后的代码,比如如果在后面同时调用了当前文章的评论,那评论内容很可能,出现的是最后一个随机到的文章的评论,而非当前文章的评论。 7 j! k( \7 S9 V0 `' t, X
方法二:在随机文章中显示标题和文章摘要在需要显示的地方直接调用如下代码
: X$ \- y3 [$ O7 N- <?php9 z$ V+ y }& b
- query_posts(array(‘orderby’ => ‘rand’, ‘showposts’ => 1));
2 O5 ?8 A- ^* ^1 z$ G4 } - if (have_posts()) :
' |; H- U1 h8 k: ] x) r% D) G8 H1 S* h - while (have_posts()) : the_post();
7 x' M. e. P( c" [$ @- Q6 z - the_title(); //这行去掉就不显示标题1 z0 ?" Y, G. p7 c$ F, O Q- u5 P
- the_excerpt(); //去掉这个就不显示摘要了
! a1 @$ U6 e: X - endwhile; A+ Y$ Y6 {- X1 h
- endif; ?>
复制代码 方法三:用query_posts生成随机文章列表在需要显示的地方直接调用如下代码 - <?php
0 }1 `' V0 c T0 ^ ^ - query_posts(array(‘orderby’ => ‘rand’, ‘showposts’ => 2));; ]4 Q0 @) `! q+ r1 m5 F
- if (have_posts()) :8 E3 b& [9 `- w' t
- while (have_posts()) : the_post();?>
0 G @0 m2 y5 c: X1 J - <a href=”<?php the_permalink() ?>”
* Y. e% y9 j+ a" A, k - rel=”bookmark”! M: b9 {1 E0 U, ^
- title=<?php the_title(); ?>”><?php the_title(); ?></a>
' K- z' q& E# K% }7 s8 S1 o, M - <?php comments_number(”, ‘(1)’, ‘(%)’); ?># c+ k2 W2 \) v' _1 B$ Z% X" F
- <?php endwhile;endif; ?>
复制代码 方法四:在function.php中加入如下代码在function.php中加入如下代码 - /**
9 n6 q4 s9 }" c$ D% c m- m - * 随机文章
4 J8 }8 [9 X. O8 p( e - */8 w( B3 l. ~/ D, J
- function random_posts($posts_num=5,$before='<li>',$after='</li>'){7 A0 `5 \; Q* c( p. Q7 `9 m
- global $wpdb;. I* M- A- U! q! u, H% k
- $sql = "SELECT ID, post_title,guid- Z5 F- j, E, w; J5 @' \/ q( ~
- FROM $wpdb->posts
, U! z& n# \ Z! \ - WHERE post_status = 'publish' ";
6 P I( `- d" F - $sql .= "AND post_title != '' ";
& K* y) V3 }% L) a- K: p& D - $sql .= "AND post_password ='' ";
3 q3 h' u9 f$ ]. m' X+ T - $sql .= "AND post_type = 'post' ";
) j' F- ~* w4 q9 ~, i - $sql .= "ORDER BY RAND() LIMIT 0 , $posts_num ";
S a4 q( W# N, A1 [' J B - $randposts = $wpdb->get_results($sql);, `9 }$ V( A" O& R) z: G* p
- $output = '';/ P# @% X) @- a7 c8 ]
- foreach ($randposts as $randpost) {
; l( {$ Q' ?9 g: h4 X - $post_title = stripslashes($randpost->post_title);6 ^4 a+ I a: I
- $permalink = get_permalink($randpost->ID);- G* w" @0 L+ g0 y: S
- $output .= $before.'<a href="'
& M5 ?5 S5 H& t# C4 ]: a1 X& V, m4 p - . $permalink . '" rel="bookmark" title="';. A0 x7 o8 W W& B" b/ F6 g
- $output .= $post_title . '">' . $post_title . '</a>';3 Y# y7 Y% h' h, n
- $output .= $after;$ `6 o& q' h- s1 d& |6 |
- }1 m* L' \ J. S9 C
- echo $output;0 q6 N8 c2 x$ J% g) s
- }
+ n9 B9 q, T) ^ - 在需要显示的地方调用如下代码
% X& m) G0 V6 ~" n0 R - <div>: @* o2 z# ~1 V: C! ?
- <h3>随便找点看看!</h3>
# b. g" I a* Z; l' v8 o - <ul>
! r9 R( N! C$ b4 l$ \* S - <?php random_posts(); ?>8 i. h3 D; L# w) r
- </ul>$ D0 i* p, J7 J$ ~" P% d
- </div><!-- 随机文章 -->
复制代码 ; H, d( [) I0 | }+ [: @
% P8 W/ a) g9 g) L5 f/ I# Q' I" g
9 b/ J" f4 |2 p0 G9 n |