我们的主机更新了 PHP,导致我们的一些东西无法工作。我知道 create_function 不再有效,但我很难转换它。有人可以帮忙吗?
add_filter('single_post_template', create_function(
'$the_template',
'foreach( (array) get_the_category() as $cat ) {
if ( file_exists(TEMPLATEPATH . "/single-{$cat->slug}.php") )
return TEMPLATEPATH . "/single-{$cat->slug}.php"; }
return $the_template;' )
);
您只需要创建一个匿名函数即可:
// uses a static function to pass $the_template to the filter.
add_filter( 'single_post_template', static function ( $the_template ) {
// Uses a variable instead to hold the categories.
$cats = get_the_category();
foreach ( $cats as $cat ) :
if ( get_template_directory() . '/single-' . $cat->slug . '.php' ) :
return get_template_directory() . '/single-' . $cat->slug . '.php';
endif;
endforeach;
return $the_template;
});