下一段代码给我一个错误。 init钩子很早就可以访问:array($ post-> ID)。如果我使用'wp'钩子,'var_dump($ all_dash_for);'总是返回'null'。
我在管理区域中使用此代码作为插件元数据箱。当我正在编辑自定义帖子类型时,我想要排除查询的帖子ID。有任何想法吗? thx
function dash_for() {
global $all_dash_for, $post;
$all_dash_for = array();
$args = array(
'post_type' => 'sliding_panel',
'post__not_in' => array($post->ID)
);
// Query the posts:
$panel_for_query = new WP_Query($args);
while ($panel_for_query->have_posts()) : $panel_for_query->the_post();
$dash_for = get_post_custom_values('_dash_for');
if(!empty($dash_for)){
array_push($all_dash_for, $dash_for[0]);
}
endwhile;
// Reset Post Data
wp_reset_postdata();
if ( empty( $all_dash_for ) ) {
array_push($all_dash_for, 'empty');
}
var_dump($all_dash_for);
}
add_action( 'init', 'dash_for' );