我尝试在customize_register操作下获取当前的queried_object ID ...
// call action customize register
add_action('customize_register','register_customize_options');
//customize register callback function
function register_customize_options( $wp_customize ){
//if is page customize preview
if( is_page() ): //not work
$ObjID = get_the_ID(); //not work
//if is any taxonomy customize preview
elseif ( is_tax() ) : //not work
$ObjID = get_queried_object()->term_id; //not work
endif;
// obj $wp_customize;
// var_dump( $wp_customize ); //not found any ID
//rest of code...
}我还尝试了通过url_to_postid( $_GET['url'] )获取页面ID,即只要用户在页面上打开自定义即可。
有什么建议如何在customize_register操作下动态获取对象ID吗?
发布于 2017-07-20 15:35:02
如何获取帖子、页面或分类ID?您可以在function.php中添加以下函数
//customize register callback function
function register_customize_options( $wp_customize ){
if(is_page() || is_single() )
{
$ObjID = get_the_ID();
}
$term = get_queried_object();
if ($term )
{
if ( is_category() || is_tag() || is_tax()) {
$ObjID = $term->term_id;
}
}
return $ObjID;
// obj $wp_customize;
// var_dump( $wp_customize ); //not found any ID
//rest of code...
}
add_action('customize_register','register_customize_options');https://stackoverflow.com/questions/45204285
复制相似问题