• I’m trying to call category in wordpress customizer and show sub category as tree like this

    Cat 1
    Parent cat
    -sub cat
    -Sub cat
    –sub 2

    Tree category
    Hope you get what I’m asking here is my code I’m using but it display as list only.

    if ( ! class_exists( 'WP_Customize_Control' ) )
        return NULL;
    
    /**
     * A class to create a dropdown for all categories in your wordpress site
     */
     class Category_Dropdown_Custom_Control extends WP_Customize_Control
     {
        private $cats = false;
    
        public function __construct($manager, $id, $args = array(), $options = array())
        {
            $this->cats = get_categories($options);
    
            parent::__construct( $manager, $id, $args );
        }
    
        /**
         * Render the content of the category dropdown
         *
         * @return HTML
         */
        public function render_content()
           {
                if(!empty($this->cats))
                {
                    ?>
                        <label>
                          <span class="customize-category-select-control"><?php echo esc_html( $this->label ); ?></span>
                          <select <?php $this->link(); ?>>
                               <?php
                                    foreach ( $this->cats as $cat )
                                    {
                                        printf('<option value="%s" %s>%s</option>', $cat->term_id, selected($this->value(), $cat->term_id, false), $cat->name);
                                    }
                               ?>
                          </select>
                        </label>
                    <?php
                }
           }
     }
Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    You need code to differentiate child categories. Why not use wp_dropdown_categories()? If not directly, then review its source code to see how it’s done.

    Thread Starter Sandy

    (@sandy786)

    But No success on implement. can any one help on this

    Current Codes

     
    if ( ! class_exists( 'WP_Customize_Control' ) )
        return NULL;
    
    /**
     * A class to create a dropdown for all categories in your wordpress site
     */
     class Category_Dropdown_Custom_Control extends WP_Customize_Control
     {
        private $cats = false;
    
        public function __construct($manager, $id, $args = array(), $options = array())
        {
            $this->cats = get_categories($options);
    
            parent::__construct( $manager, $id, $args );
        }
    
        /**
         * Render the content of the category dropdown
         *
         * @return HTML
         */
        public function render_content()
           {
                if(!empty($this->cats))
                {
                    ?>
                        <label>
                          <span class="customize-category-select-control"><?php echo esc_html( $this->label ); ?></span>
                          <select <?php $this->link(); ?>>
                               <?php
                                    foreach ( $this->cats as $cat )
                                    {
                                        printf('<option value="%s" %s>%s</option>', $cat->term_id, selected($this->value(), $cat->term_id, false), $cat->name);
                                    }
                               ?>
                          </select>
                        </label>
                    <?php
                }
           }
     }

    And array

    // create an empty array
    $cats = array();
     
    // we loop over the categories and set the names and
    // labels we need
    foreach ( get_categories() as $categoriesi => $categoryy ){
        $cats[$categoryy->term_id] = $categoryy->name;
    }

    Into customizer

     $wp_customize->add_control( new Category_Dropdown_Custom_control( $wp_customize, 'category_dropdown_setting', array(
                'label'   => 'Select A Category',
                'section' => 'category_demo_section',
                'settings'   => 'category_dropdown_setting',
            ) ) );

    Now I’m trying to use wp_dropdown_categories

    // create an empty array
    $cats = array();
     
    // we loop over the categories and set the names and
    // labels we need
    foreach ( wp_dropdown_categories() as $categoriesi => $categoryy ){
        $cats[$categoryy->term_id] = $categoryy->name;
    }

    And custom walker doesn’t seems to work

    Moderator bcworkz

    (@bcworkz)

    For starters, drop down categories by default outputs content, it does not return values. You can get it to return content with the ‘echo’ argument, but it’ll not be an array of category terms like you are trying to use. It returns a dropdown HTML string.

    You ought to be able to simply call this function in your customizer render callback if the function’s normal output is acceptable. No need to loop through terms. If not, the output can be altered in several ways: by certain arguments, by certain filters, and if necessary, a custom walker. A custom walker is rarely needed, but it offers the ultimate in flexibility. What is it about the default output you are trying to change?

    I don’t know if the code you presented has other issues. Until wp_dropdown_categories() is used properly, it’s difficult to identify any other issues.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How Category to Display Like Tree in Customizer’ is closed to new replies.