具有动态生成的名称作为函数参数的 Wordpress 瞬变
Wordpress transients with dynamically generated name as parameter in function
希望你能帮助我解决这个问题:
我在短代码函数中有几个不同 post 类型的查询。
现在我试图用瞬态存储这些查询。
但是这些瞬态需要为调用短代码的每个页面都有一个唯一的名称。
$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 );
}
编辑:Transient 没有减少查询,尽管 transient 似乎被正确调用。有人有想法吗?
解决方案是将查询 和 循环放在瞬态中。
有两种可能的方法:
函数内部(短代码 f.e。)
function get_content( $dynamic_var ){
$transient_time = 60*60*4;
$transient_name = "transient_name_" . $dynamic_var;
$content = get_transient( $transient_key );
if( !empty($content) ) { return $content; }
$args = array ('');
$query = new WP_Query( $args );
$content = '';
if( $query->have_posts() ):
while( $query->have_posts() ) : $query->the_post();
$content.= 'some_content';
endwhile; wp_reset_postdata();
endif;
set_transient( $transient_name, $content, $transient_time );
return $content;
}
在模板中
$transient_time = 60*60*4;
$transient_name = 'transient_name_' . $page_id;
// ${$transient_name} > name of variable is dynamically created by
// the value of variable $transient_name (search for > php variable variables)
$content = '';
if( false === ( ${$transient_name} = get_transient( $transient_name ) ) ) {
$args = array ('');
$query = new WP_Query( $args );
$content_inner = '';
if( $query->have_posts() ):
while( $query->have_posts() ) : $query->the_post();
$content_inner.= 'some_content';
endwhile; wp_reset_postdata();
endif;
set_transient( $content_inner, ${$transient_name}, $transient_time );
}
$content.= ${$transient_name};
希望你能帮助我解决这个问题:
我在短代码函数中有几个不同 post 类型的查询。 现在我试图用瞬态存储这些查询。 但是这些瞬态需要为调用短代码的每个页面都有一个唯一的名称。
$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 );
}
编辑:Transient 没有减少查询,尽管 transient 似乎被正确调用。有人有想法吗?
解决方案是将查询 和 循环放在瞬态中。 有两种可能的方法:
函数内部(短代码 f.e。)
function get_content( $dynamic_var ){ $transient_time = 60*60*4; $transient_name = "transient_name_" . $dynamic_var; $content = get_transient( $transient_key ); if( !empty($content) ) { return $content; } $args = array (''); $query = new WP_Query( $args ); $content = ''; if( $query->have_posts() ): while( $query->have_posts() ) : $query->the_post(); $content.= 'some_content'; endwhile; wp_reset_postdata(); endif; set_transient( $transient_name, $content, $transient_time ); return $content; }
在模板中
$transient_time = 60*60*4; $transient_name = 'transient_name_' . $page_id; // ${$transient_name} > name of variable is dynamically created by // the value of variable $transient_name (search for > php variable variables) $content = ''; if( false === ( ${$transient_name} = get_transient( $transient_name ) ) ) { $args = array (''); $query = new WP_Query( $args ); $content_inner = ''; if( $query->have_posts() ): while( $query->have_posts() ) : $query->the_post(); $content_inner.= 'some_content'; endwhile; wp_reset_postdata(); endif; set_transient( $content_inner, ${$transient_name}, $transient_time ); } $content.= ${$transient_name};