选择wordpress中的随机帖子之一

时间:2014-11-03 22:40:48

标签: wordpress random

我想在wordpress中制作一个插件,比如测验但不是特别的问题。只有帖子。我将在这4个帖子之间获得4个帖子和1个特色图像。客人将选择特色图片的正确标题。我随机收到了帖子;

<ul>
<?php $posts = get_posts('orderby=rand&numberposts=4'); foreach($posts as $post) { ?>
<li><p desc="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></p>
</li>
<?php } ?>
</ul>

但我无法解决我将如何随机选择正确答案并将其作为问题的特色图片。

请帮助

1 个答案:

答案 0 :(得分:0)

<ul>
    <?php
        $arr = array();  
        $posts = get_posts('orderby=rand&numberposts=4');
        foreach($posts as $post) { ?>
        <li>
            <p desc="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?>    </p>
            <?php $arr[ get_the_ID() ]; // store every post id in array ?>
        </li>
    <?php } ?>
</ul>
<?php 
    $id = array_rand( $arr ); // choose one random post id
    echo get_the_post_thumbnail( $id,'thumbnail' ); // get thubnail against id 
?>

使用此代码并将用户选择的帖子ID与“$ id”值进行比较。

干杯!!!