1

我試圖讓customize_register作用下,當前queried_object ID ...如何下ADD_ACTION拿到後,頁面或分類ID( 'customize_register',...) - WP定製

// 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... 

} 

而且我試着讓頁面ID由url_to_postid($_GET['url']),這是工作只是如果用戶打開頁面上自定義。

任何建議如何在customize_register動作下動態獲取對象ID?

回答

0

如何獲取帖子,頁面或分類標識? 你可以在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'); 
+0

感謝您的更新,但這對我不起作用。 您是否嘗試過在您的WP安裝? –