$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - How do I get current page ID in Wordpress customizer file?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - How do I get current page ID in Wordpress customizer file?

matteradmin11PV0评论

I'm trying to hide or show customizer settings based on what page I am viewing similar to active_callback' => 'is_front_page', however, I haven't found a way how to access the current page ID from my customizer.php file. I want to be able to use active_callback' => 'is_specific_page' through a custom callback based on current page ID like so:


    function is_specific_page() {

        // LOGIC RETURNS TRUE OR FALSE DEPENDING ON CURRENT PAGE

    }    

I've tried using all of the following to no avail:

global $post; $post->ID();

global $wp_query; $post_id = $wp_query->post->ID;

get_the_ID();

Thank you in advanced for your help!

I'm trying to hide or show customizer settings based on what page I am viewing similar to active_callback' => 'is_front_page', however, I haven't found a way how to access the current page ID from my customizer.php file. I want to be able to use active_callback' => 'is_specific_page' through a custom callback based on current page ID like so:


    function is_specific_page() {

        // LOGIC RETURNS TRUE OR FALSE DEPENDING ON CURRENT PAGE

    }    

I've tried using all of the following to no avail:

global $post; $post->ID();

global $wp_query; $post_id = $wp_query->post->ID;

get_the_ID();

Thank you in advanced for your help!

Share Improve this question asked Dec 5, 2018 at 2:43 Alex E.Alex E. 111 bronze badge 4
  • please check url and try: rudrastyh/wordpress/get-post-id.html – vikrant zilpe Commented Dec 5, 2018 at 5:29
  • 1 @vikrantzilpe sorry, but how is your comment related to this question? This article has nothing to do with Customizer... – Krzysiek Dróżdż Commented Dec 5, 2018 at 7:13
  • To me this seem to be a duplicate of wordpress.stackexchange/questions/298752/… – JHoffmann Commented Dec 5, 2018 at 9:42
  • @JHoffmann do you know a way I could implement this solution in vanilla JS? – Alex E. Commented Dec 7, 2018 at 18:02
Add a comment  | 

1 Answer 1

Reset to default 1

Thanks for your question.

active_callback is exactly what you're looking for. You can use it with controls:

$wp_customizer->add_control(
    'setting_name',
    array(
        'type' => 'text',
        'section' => 'section_name',
        'label' => 'Option with context',
        'active_callback' => 'is_front_page'
    )
);

and with sections:

$wp_customize->add_section(
    'section-name',
    array(                                             
        'title' => 'Section with context',
        'active_callback' => 'is_front_page'
     )
);

In examples above, this new setting/section will be visible only for front page, thanks to native is_front_page function. You can also use other Conditional Tags.

But of course you can make your own contexts:

function mytheme_is_contact_page() {
    return is_page_template( 'template-contact.php' );
}

function mytheme_is_page_id_123() {
    return is_page( 123 );
}
Post a comment

comment list (0)

  1. No comments so far