【发布时间】:2015-12-03 03:57:23
【问题描述】:
我有这两个 WordPress 功能:
$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。
【问题讨论】:
-
function ($post_id)之后需要添加use (wpb_set_post_views)。变量超出范围。 -
@Andrew 你是说
use ($wpb_set_post_views)吗? -
是的,我的错,忘了
$。 -
那么你真的需要这个匿名函数吗?
-
您找到问题的答案了吗?