随机显示wordpress中帖子的附件

时间:2013-12-26 04:49:44

标签: wordpress

这是我的实际代码:

<?php
if (have_posts()) :
$c = 0; while ( have_posts() ) : the_post(); $c++;
if ( $c == 1) {
$style = "current first";
}
elseif ( $c == count( $posts ) ) {
$style = "last"; } /*
elseif ( $c == 2 ) {
$style = ""; }*/

else $style ='dispa';


$images =& get_children( array (
'post_parent' => $post->ID,
'post_type' => 'attachment'

));
foreach ( $images as $attachment_id => $attachment ) {

?><div <?php post_class($style) ?>id ="post_<?php echo $post->ID;?>"><?php echo wp_get_attachment_image( $attachment_id, 'large' ); }

?><div <?php post_class('mezzanine ' . $style) ?>><?php $description = $attachment->post_content;
echo $description;
?></div></div><?php
endwhile;
wp_reset_postdata();
else :
echo wpautop( 'Sorry, no posts were found' );
endif;
?>

这样做的结果是显示直接附加到div中当前帖子的附件图像。当前帖子附件的描述被检索到另一个div(#mezzanine)。 我想以随机顺序显示帖子附件,但是当我使用'orderby'=&gt; 'rand'订单没有变化。 有谁看到可能来自哪里? 提前谢谢

2 个答案:

答案 0 :(得分:0)

尝试使用rand这样的

'orderby' => 'rand()'

可能对你有用

$query = new WP_Query( array('post_type' => 'attachment', 'post_parent' => $post->ID ) ); 
$key = array_rand($query->posts, 1);

echo wp_get_attachment_image($query->posts[$key]->ID, 'medium');

答案 1 :(得分:0)

这似乎是答案?
http://php.quicoto.com/how-to-get-random-post-attachments-wordpress/

我要自己测试一下。

相关问题