Random post, once per day

时间:2015-08-25 作者:cubechris

我有一个自定义的帖子类型“quotes”,我想每天在主页上显示一个随机条目。

这是我当前必须显示内容的代码:

            <?php query_posts( \'post_type=quote&orderby=rand&showposts=1\' ); ?>
            <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
            <?php if ( has_post_thumbnail() ) {
                the_post_thumbnail( \'thumbnail-quote\' );
                } else { ?>
                <img src="<?php bloginfo(\'template_directory\'); ?>/img/thumb1.jpg" alt="x" class="quote_person" />
                <?php } ?>
        <div class="quote_container">
            <span class="quote_intro hstack">Quote of the day:</span><a class="quote_tweet hstack" href="#">Tweet this quote <i class="fa fa-twitter"></i></a>
            <div class="quote_message white gstack"><?php the_field(\'quote\'); ?></div>
            <div class="quote_source white hstack"><?php the_field(\'attribution\'); ?></div>
        </div>
        <?php endwhile; endif; ?>
EDIT:

根据这些有用的建议,我现在有如下内容:

            <?php 

        if ( false === ( $quotes = get_transient( \'random_quote\' ) ) ) {
          // It wasn\'t there, so regenerate the data and save the transient

          $args = array(
             \'post_type\' => \'quote\',
             \'orderby\'   => \'rand\',
             \'posts_per_page\' => \'1\'
          );

          $quotes = get_posts( $args );

          //Now we store the array for one day.
          //Just change the last parameter for another timespan in seconds.
          $seconds_until_next_day = strtotime(\'tomorrow\') - time();
          set_transient( \'random_quote\', $quotes, $seconds_until_next_day );
        }

        foreach ( $quotes as $post ) : setup_postdata( $post );
        if ( have_posts()) : while (have_posts()) : the_post();

            if ( has_post_thumbnail() ) {
            the_post_thumbnail( \'thumbnail-quote\' );
            } else { \'<img src="\' . bloginfo(\'template_directory\') .\'/img/thumb1.jpg" alt="University of Lincoln" class="quote_person" />\'
            echo \'<div class="quote_container">
                <span class="quote_intro hstack">Quote of the day:</span><a class="quote_tweet hstack" href="#">Tweet this quote <i class="fa fa-twitter"></i></a>
                <div class="quote_message white gstack" id="thequote">\' . the_field(\'quote\') . \'</div>
                <div class="quote_source white hstack">\' . the_field(\'attribution\') . \'</div>
            </div>
            \'; }

        endforeach; 
        wp_reset_postdata();


        ?>
我不太确定foreach的说法是否正确。

1 个回复
最合适的回答,由SO网友:kraftner 整理而成

首先,你真的不应该使用query_posts(). Read this excellent explanation why.

那么这是一个完美的用例transients.

您只需获取一次帖子,然后使用Transients API将其缓存24小时。

if ( false === ( $quotes = get_transient( \'random_quote\' ) ) ) {
  // It wasn\'t there, so regenerate the data and save the transient

  $args = array(
     \'post_type\' => \'quote\',
     \'orderby\'   => \'rand\',
     \'posts_per_page\' => \'1\'
  );

  $quotes = get_posts( $args );

  //Now we store the array for one day.
  //Just change the last parameter for another timespan in seconds.
  set_transient( \'random_quote\', $quotes, DAY_IN_SECONDS );
}
如果您的目标是将其绑定到日历日,而不是最后一个请求后的24小时,请将最后一行替换为以下内容。(propschaos)

$seconds_until_next_day = strtotime(\'tomorrow\') - time();
set_transient( \'random_quote\', $quotes, $seconds_until_next_day );
获得数据后,您可以按任何方式显示数据:

foreach ( $quotes as $post ) : setup_postdata( $post );

  [...]
  the_title();
  [...]

endforeach; 
wp_reset_postdata();

See this link for more examples.

EDIT:

这应该在您的特定情况下起到作用:

foreach ( $quotes as $post ) : setup_postdata( $post );

    if ( has_post_thumbnail() ) {
                the_post_thumbnail( \'thumbnail-quote\' );
    } else { ?>
            <img src="<?php echo get_stylesheet_directory_uri (); ?>/img/thumb1.jpg" alt="x" class="quote_person" />
    <?php } ?>
    <div class="quote_container">
        <span class="quote_intro hstack">Quote of the day:</span><a class="quote_tweet hstack" href="#">Tweet this quote <i class="fa fa-twitter"></i></a>
        <div class="quote_message white gstack"><?php the_field(\'quote\'); ?></div>
        <div class="quote_source white hstack"><?php the_field(\'attribution\'); ?></div>
    </div>

<?php endforeach; 
wp_reset_postdata();

相关推荐

将wp_Query替换为wp_User_Query

我在插件中制作了一些订阅者档案和单曲(个人资料页),其中我还包括了一个“用户单曲”模板,通过template_include. 不过,我正在尝试从插件中删除一些模板,以使其使用主题模板。我用了locate_template( \'single.php\' ) 从活动主题中选择单个模板。我没有使用全局wp_query 在本例中显示我的内容,但页面显示了基于查询默认值的循环(十篇帖子)。我想知道的是,我是否可以完全放弃默认查询,用wp_user_query 我可以将查询到的用户ID输入其中。然后我想筛选the