0
WordPress的瞬态
希望你可以帮我这样的场景:动态生成的名称作为函数参数
我有不同的文章类型简码功能里面的几个查询。 现在我试图用瞬变来存储这些查询。 但是这些瞬变需要为每个页面调用shortcode的唯一名称。
$trans_posts_golfcourse_ = 'trans_posts_golfcourse_'.$landingpage;
if(false === ($$trans_posts_golfcourse_ = get_transient('trans_posts_golfcourse_'))) {
$args = array (
'posts_per_page'=> 5,
'post__in' => $posts_golfcourse,
'post_type' => 'golfcourse',
'post_status' => 'publish',
'cache_results' => false,
);
$$trans_posts_golfcourse_ = new WP_Query($args);
set_transient('trans_posts_golfcourse_', $$trans_posts_golfcourse_, 60*60*4);
}
动态生成的变量名是
$$trans_posts_golfcourse_
但如何,这都看起来像作为参数?:提前
get_transient('trans_posts_golfcourse_')
谢谢!
编辑:用于动态可变发现溶液作为参数 参数(串)具有将要产生的方式作为变量名相同:
get_transient('trans_posts_golfcourse_'.$landingpage)
完整代码:
$trans_posts_golfcourse_ = 'trans_posts_golfcourse_'.$landingpage;
if(false === (${$trans_posts_golfcourse_} = get_transient('trans_posts_golfcourse_'.$landingpage))) {
$args = array (
'posts_per_page'=> 5,
'post__in' => $posts_golfcourse,
'post_type' => 'golfcourse',
'post_status' => 'publish',
'cache_results' => false,
);
${$trans_posts_golfcourse_} = new WP_Query($args);
set_transient('trans_posts_golfcourse_'.$landingpage, ${$trans_posts_golfcourse_}, 60*60*4);
}
编辑:瞬态不减少查询,虽然瞬态似乎被正确调用。有人有想法吗?
添加您的完整循环,一切都在瞬间内。看看[这篇文章](http://wordpress.stackexchange.com/a/160618/31545)我已经完成了[wordpress.se]。请注意,您应该创建一个字符串,而不是像示例中的数组,因为简码不能返回数组,只是字符串 –
感谢Pieter!这是绝对正确的。我昨晚通过以下方式管理它: '\t $ transient_key ='trans_markers_'。 $ PAGE_ID; \t $ transient = get_transient($ transient_key); \t if(!empty($ transient)){return $ transient; } \t ...做...查询 \t set_transient($时,$ transient_key,$ transient_time); \t return $ out;' – Funkateer
您应该将其作为答案发布。评论中的代码完全不可读,因此在评论中毫无用处。 ;-) –