【问题标题】:How do I get random excerpts to show in WordPress?如何获得随机摘录以在 WordPress 中显示?
【发布时间】:2014-09-08 14:18:06
【问题描述】:

早上好,我一直在尝试获取帖子的摘录,以便在主页上显示随机的帖子摘录,但没有成功。我的代码如下所示:

    <?php
        $spotlight  = new WP_Query( array( 'post_type' => 'success-spotlight', 'posts_per_page' => 1,       'orderby' => 'rand' ) );
while ( $spotlight->have_posts() ) : $spotlight->the_post();
    $spotlight_title = get_the_title();
    $spotlight_excerpt = types_render_field("story-headline", array("raw"=>"true"));
    $spotlight_link = get_permalink();
    if (has_post_thumbnail()) {
        $thumb = wp_get_attachment_image_src(get_post_thumbnail_id(), 'spiffy-success-spotlight-`home');
        $spotlight_image = $thumb[0]; // thumbnail url
    }


endwhile; 
wp_reset_postdata();
?>
<?php

?>

任何建议将不胜感激,并在此先感谢您

【问题讨论】:

  • 到底是什么问题?在您的WP_Query 中,您指定只获取一个帖子,因此它不会是随机的
  • @AhmadAssaf 非常感谢
  • @AhmadAssaf 即使我将 posts_per_page 更改为更高的数字(例如 5),它也不会随机显示帖子
  • 你能更具体地说明你想要什么。所以你想显示你帖子的 N 个随机摘录?
  • @AhmadAssaf 我需要一个帖子来显示,但随机有大约 15 个不同的帖子作为成功的焦点。每次主页加载时,我都需要随机显示 1。目前同一篇文章仅在主页加载时显示。

标签: php arrays wordpress


【解决方案1】:

怎么样:

query_posts('orderby=rand&showposts=1&post_type=success-spotlight');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-16
    • 2013-10-23
    相关资源
    最近更新 更多