首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在add_action(' Customize _register',...)下获取帖子、页面或分类ID - WP自定义

如何在add_action(' Customize _register',...)下获取帖子、页面或分类ID - WP自定义
EN

Stack Overflow用户
提问于 2017-07-20 10:52:47
回答 1查看 261关注 0票数 1

我尝试在customize_register操作下获取当前的queried_object ID ...

代码语言:javascript
复制
// 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吗?

EN

Stack Overflow用户

发布于 2017-07-20 15:35:02

如何获取帖子、页面或分类ID?您可以在function.php中添加以下函数

代码语言:javascript
复制
//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');
票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45204285

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档