希望你能帮助我解决这个问题:
我在简码函数中有几个不同帖子类型的查询。现在我正在尝试将这些查询与瞬态一起存储。但是这些瞬态需要为调用短代码的每个页面都有一个唯一的名称。
$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 );
}
编辑:瞬态并没有减少查询,尽管瞬态似乎被正确调用。有人有想法吗?