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!
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 );
}