我在这两个WordPress的功能:PHP匿名函数:未定义的变量
$wpb_set_post_views = function($postID) {
$count_key = 'wpb_post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count==''){
$count = 0;
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
}else{
$count++;
update_post_meta($postID, $count_key, $count);
}
};
add_action('wp_head', function ($post_id) {
if (!is_single()) return;
if (empty ($post_id)) {
global $post;
$post_id = $post->ID;
}
$wpb_set_post_views($post_id);
});
但最后一行返回页面Notice: Undefined variable: wpb_set_post_views
。
后'函数(POST_ID $)'你需要添加'使用(wpb_set_post_views)'。该变量超出了范围。 – Andrew
@Andrew你的意思是'use($ wpb_set_post_views)'? – marcelo2605
是的,我的坏,忘了'$'。 – Andrew