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

admin2025-06-05  0

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 );
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749127653a316599.html

最新回复(0)