Last active
December 9, 2021 08:05
-
-
Save anastis/57bb4a3205902295734611858b543390 to your computer and use it in GitHub Desktop.
WordPress Customizer Categories dropdown https://www.cssigniter.com/wordpress-customizer-custom-controls-categories-dropdown/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class My_Dropdown_Category_Control extends WP_Customize_Control { | |
public $type = 'dropdown-category'; | |
protected $dropdown_args = false; | |
protected function render_content() { | |
?><label><?php | |
if ( ! empty( $this->label ) ) : | |
?><span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span><?php | |
endif; | |
if ( ! empty( $this->description ) ) : | |
?><span class="description customize-control-description"><?php echo $this->description; ?></span><?php | |
endif; | |
$dropdown_args = wp_parse_args( $this->dropdown_args, array( | |
'taxonomy' => 'category', | |
'show_option_none' => ' ', | |
'selected' => $this->value(), | |
'show_option_all' => '', | |
'orderby' => 'id', | |
'order' => 'ASC', | |
'show_count' => 1, | |
'hide_empty' => 1, | |
'child_of' => 0, | |
'exclude' => '', | |
'hierarchical' => 1, | |
'depth' => 0, | |
'tab_index' => 0, | |
'hide_if_empty' => false, | |
'option_none_value' => 0, | |
'value_field' => 'term_id', | |
) ); | |
$dropdown_args['echo'] = false; | |
$dropdown = wp_dropdown_categories( $dropdown_args ); | |
$dropdown = str_replace( '<select', '<select ' . $this->get_link(), $dropdown ); | |
echo $dropdown; | |
?></label><?php | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action( 'customize_register', 'olsen_light_child_customize_register' ); | |
function olsen_light_child_customize_register( WP_Customize_Manager $wp_customize ) { | |
require_once get_stylesheet_directory() . '/customizer-controls/dropdown-category.php'; | |
$wp_customize->add_section( 'homepage', array( | |
'title' => esc_html_x( 'Homepage Options', 'customizer section title', 'olsen-light-child' ), | |
) ); | |
$wp_customize->add_setting( 'home_slider_category', array( | |
'default' => 0, | |
'sanitize_callback' => 'absint', | |
) ); | |
$wp_customize->add_control( new My_Dropdown_Category_Control( $wp_customize, 'home_slider_category', array( | |
'section' => 'homepage', | |
'label' => esc_html__( 'Slider posts category', 'olsen-light-child' ), | |
'description' => esc_html__( 'Select the category that the slider will show posts from. If no category is selected, the slider will be disabled.', 'olsen-light-child' ), | |
// Uncomment to pass arguments to wp_dropdown_categories() | |
//'dropdown_args' => array( | |
// 'taxonomy' => 'post_tag', | |
//), | |
) ) ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment