我已经看过很多关于如何做到这一点的文章,但是经过几个小时的尝试使用瞬态实现这一点,我似乎没有更接近梦想!
基本上我想使用wordpress瞬态来获得3个随机帖子并将其显示在一个特色的'模板24小时在我的主页上。这3个帖子需要混合使用本机和自定义帖子类型。
我到目前为止的代码是:
<div class="container">
<div class="featured-wrapper">
<div class="section-title">featured</div>
<?php
if ( ( $my_query = get_transient('my_query_cached') ) === false ) :
global $wp_query;
$args = array_merge( $wp_query->query, array(
'post_type' => array('post', 'recipe'),
'posts_per_page' => 3,
'orderby' => 'rand'
)
);
$my_query = new WP_Query($args);
set_transient('my_query_cached', $my_query, 24 * HOUR_IN_SECONDS);
endif;
?>
<?php if ( $my_query->have_posts() ) :
while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
<div class="card-wrapper">
<a href="<?php the_permalink(); ?>">
<div class="card-img">
<img src="<?php the_post_thumbnail(); ?>"/>
</div>
</a>
<div class="card-cut"></div>
<div class="card-content">
<a href="category.html"><span class="card-category">treats</span></a>
<h1><a href="recipe.html"><?php echo get_the_title(); ?></a></h1>
</div>
</div><!--END card wrapper 1-->
<?php endwhile; wp_reset_postdata(); wp_reset_query(); ?>
<?php else: ?>
<div>
<h1>Sorry...</h1>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
</div>
<?php endif; ?>
</div><!-- END featured wrapper-->
目前这段代码正在返回数据库中的所有帖子,所以我相信第一部分存在问题,但似乎无法将其放入其中。
非常感谢您提前寻求帮助。
更新
固定。上面的代码实际上很好,但值得记住的是,一旦页面已经运行一次因为已经设置了瞬态,任何编辑都会显示WON&#t; T。值得将刷新率设置为1 * MINUTE_IN_SECONDS进行测试。
答案 0 :(得分:0)
我将您的代码粘贴到我的某个网站中&#39; frontpage.php,它直接工作得很好。将瞬态过期时间更改为5或10秒进行测试。