• Resolved iwtravelllc

    (@iwtravelllc)


    As the title of this topic says, it appears that even when you have “include subcategory” unchecked that the grandchildren of the “parent term id” are also included. I would think only the immediate, first-level children would be included.

    Ok so I posted a little earlier that “Include Subcategory” unchecked still includes subcategories”…I still am not sure what the “include subcategory” was supposed to do…

    Anyways, I modified the code a bit.

    1.) Removed “Include Subcategory” all together.
    2.) Created a new checkbox called “Only Display Top Level Categories”. When checked it will display only the direct, first-level children of the “parent” category that is selected. If no parent category is selected then all top-level (parentless) categories are selected.
    3.) Changed the listing of taxonomies because I have some post types that reuse the same taxonomy so those taxonomies were displaying in the drop down multiple times. For instance, if I have the post types “page”, “post”, and “product” all using the taxonomy “destination”, then “destination” would appear 3 times in the list.
    4.) Made some of the capitalization of variables and params and arguments consistent.

    Thank you for the plug in. Next think I will look at implementing is I have several plugins that allow for my taxonomies to include more detail and even attach a featured image similar to a normal post. I am thinking about implementing a layout selector on the visual composer form which will dictate how it looks on the site front end. Perhaps one layout to show the featured image with the description on the bottom and another layout to show the featured image with the description to the right, etc.

    Modified taxonomy-listing.php

    <?php
    
    if ( !class_exists( 'Taxonomy_Term_Addon' ) ) {
      class Taxonomy_Term_Addon {
    
        // Constructor for the taxonomy term listing. Loads options and hooks in the init method.
        public function __construct() { 
          add_action( 'vc_before_init', array( $this, 'termlisting' ) );
          add_action( 'plugins_loaded', array( $this,'taxonomy_listing_addon_load_textdomain' ) );
        }
    
        // Include or mapping
        function termlisting() {
          vc_map( array(
            'name' => __('Taxonomy Term Listing','taxonomy-term-listing-visual-composer-addon'),
            'base' => 'taxonomy_term',
            'icon' => TAXONOMY_LISTING_ADDON_PLUGIN_URL . '/images/icon-taxonomy-listing.png',
            'class' => '',
            'category' => 'Content',
            'params' => array(
              array(
                'type' => 'taxonomy_names',
                'holder' => 'div',
                'class' => '',
                'heading' => __( '', 'taxonomy-term-listing-visual-composer-addon' ),
                'param_name' => 'taxonomy_names',
                'value' => '',
                'description' => __('Select desired taxonomy name','taxonomy-term-listing-visual-composer-addon'),
              ),
              array( 
                'type' => 'dropdown',
                'class' => '',
                'heading' => __( '', 'taxonomy-term-listing-visual-composer-addon' ),
                'param_name' => 'order',
                'value' => array(
                  'Ascending' => 'ASC',
                  'Descending' => 'DESC'
                ),
                'description' => '',
              ),
              
              array(
                'type' => 'top_level',
                'class' => '',
                'heading' => __( '', 'taxonomy-term-listing-visual-composer-addon' ),
                'param_name' => 'top_level',
                'value' => '',
                'description' => '',
                'admin_label' => 'false',
              ),
              array(
                'type' => 'count_display',
                'class' => '',
                'heading' => __( '', 'taxonomy-term-listing-visual-composer-addon' ),
                'param_name' => 'count',
                'value' => '',
                'description' => '',
                'admin_label' => 'false',
              ),
              array(
                'type' => 'hide_empty',
                'class' => '',
                'heading' => __( '', 'taxonomy-term-listing-visual-composer-addon' ),
                'param_name' => 'hide_empty',
                'value' => '',
                'description' => '',
                'admin_label' => 'false',
              ),
              array(
                'type' => 'specific_parent',
                'class' => '',
                'heading' => __('Enter Parent term Id','taxonomy-term-listing-visual-composer-addon'),
                'param_name' => 'specific_parent',
                'value' => '',
                'description' => __('include any specific parent','taxonomy-term-listing-visual-composer-addon'),
                'admin_label' => 'false',
              ),
              array(
                'type' => 'textfield',
                'class' => '',
                'heading' => __('Extra Class Name','taxonomy-term-listing-visual-composer-addon'),
                'param_name' => 'extra_class_name',
                'value' => '',
                'description' => __('For styling any particular element','taxonomy-term-listing-visual-composer-addon'),
                'admin_label' => 'false',
              ),
            )
          ) );
        }
    
        function taxonomy_name_settings_field( $settings, $value ) {
          $data = '<div class="taxonomy_name_list">' . '<select name="' . esc_attr( $settings['param_name'] ) . '" class="wpb_vc_param_value wpb-input wpb-select">';
          $data .= '<option value="">'.__('Select Taxonomy','taxonomy-term-listing-visual-composer-addon').'</option>';
          $post_types = get_post_types( array( 'public' => true ) );
    
            foreach ( $post_types as $key => $post_type_name ) {
                $taxonomy_names = get_object_taxonomies( $post_type_name );
                foreach( $taxonomy_names as $taxonomy_name ) {
                    $taxonomies[] = $taxonomy_name;
                }
            }
          
            $taxonomies = array_unique($taxonomies);
                
            foreach($taxonomies as $taxonomy ) {
                $data .= '<option value="' . $taxonomy . '"' . ( ( $taxonomy == $value ) ? 'selected' : '' ) . '>' . $taxonomy . '</option>';   
            }
            
          $data .= '</select>' . '</div>'; ?>
          <script>
          (function( $ ) {
            jQuery('.taxonomy_name_list select').change(function(){
              var taxonomyValue = {
                action: "get_taxonomy_term_id",
                postdata: jQuery('.taxonomy_name_list select').val()
              }
              jQuery.post("<?php echo admin_url('admin-ajax.php'); ?>", taxonomyValue, function( response ) {
                jQuery('.parent_id_list select').empty().append(response);        
              } ); 
            });
            if ( jQuery('.taxonomy_name_list select').val() != "" ) {
              var taxonomyValue1 = {
                action: "get_taxonomy_term_id",
                postdata_selected: jQuery('.taxonomy_name_list select').val(),
                postdata_termselected: jQuery('.parent_id_list select').val()
              }
              jQuery.post("<?php echo admin_url('admin-ajax.php'); ?>", taxonomyValue1, function( response ) {
                jQuery('.parent_id_list select').empty().append(response);        
              } ); 
            }
          })( jQuery );
          </script>
          <?php return $data;
        }
    
        function top_level_settings_field( $settings, $value ) {
          $top_level = '<div class="top-level"><input name="' . esc_attr( $settings['param_name'] ) . '" class="wpb_vc_param_value checkbox" type="checkbox" value="' . ( ( $value != "" ) ? $value : 1 ) . '" ' . ( $value == 1 ? checked : '' ) . '' . ( ( $value == "" ) ? checked : '' ) . ' >'.__('Only Display Top Level Categories','taxonomy-term-listing-visual-composer-addon'). '</div>'; ?>
          <script>
          (function( $ ) {
            jQuery( 'input[name="top_level"]' ).on( 'change', function() {
              this.value = this.checked ? 1 : 0 ;
            });
          })( jQuery );
          </script>
          <?php return $top_level;
        }
        
        
        function count_display_settings_field( $settings, $value ) {
          $include_count_display = '<div class="include-count"><input name="'. esc_attr( $settings['param_name'] ) .'" class="wpb_vc_param_value checkbox" type="checkbox" value="' . ( ( $value != "" ) ? $value : 1 ) . '" ' . ( $value == 1 ? checked : '' ) . '' . ( ( $value == "" ) ? checked : '' ) . ' >'.__('Show Count','taxonomy-term-listing-visual-composer-addon') . '</div>'; ?>
          <script>
          (function( $ ) {
            jQuery( 'input[name="count"]' ).on( 'change', function() {
              this.value = this.checked ? 1 : 0 ;
            });
          })( jQuery );
          </script>
          <?php return $include_count_display;
        }
    
        function hide_empty_settings_field( $settings, $value ){
          $hide_empty_cat = '<div class="hide_empty_main"><input name="'. esc_attr( $settings['param_name'] ) .'" class="wpb_vc_param_value checkbox" type="checkbox" value="' . ( ( $value != "" ) ? $value : 1 ) . '" ' . ( $value == 1 ? checked : '' ) . '' . ( ( $value == "" ) ? checked : '' ) . ' >'.__('Hide Empty Category','taxonomy-term-listing-visual-composer-addon') . '</div>'; ?>
          <script>
          (function( $ ) {
            jQuery( 'input[name="hide_empty"]' ).on( 'change', function() {
              this.value = this.checked ? 1 : 0 ;
            });
          })( jQuery );
          </script>
          <?php return $hide_empty_cat;
        }
    
        function specific_parent_settings_field( $settings, $value ) {
          $specific_parent = '<div class="parent_id_list">' . '<select name="'. esc_attr( $settings['param_name'] ) . '" class="wpb_vc_param_value wpb-input wpb-select">';
          $specific_parent .= '<option value="' . $value . '">'.__('Select Taxonomy First','taxonomy-term-listing-visual-composer-addon').'</option>';  
          $specific_parent .= '</select>' . '</div>';
          return $specific_parent;
        }
    
          // Load plugin textdomain.
        function taxonomy_listing_addon_load_textdomain() {
          load_plugin_textdomain( 'taxonomy-term-listing-visual-composer-addon', false, basename( dirname( __FILE__ ) ) . '/languages' );
        }
    
        // frontend view 
        function term_listing_func( $atts ) {
          $specific_parent = isset( $atts['specific_parent'] ) ? $atts['specific_parent'] : 0 ;
          $order_attr = ( isset( $atts['order'] ) ? $atts['order'] : 'ASC' );
          $taxonomy_names_attr = ( isset( $atts['taxonomy_names'] ) ? $atts['taxonomy_names'] : NULL ); 
          $class = ( isset( $atts['extra_class_name']) ? "class = ".$atts['extra_class_name'] : "");
          $arguments = array( 'hide_empty' => $atts['hide_empty'], 'order' => $order_attr, 'parent'=> $specific_parent, 'taxonomy' => $taxonomy_names_attr); 
          $term = get_terms($arguments);
    
          if (!$atts['top_level']) {
            $response = '';
            $response = '<div class="vc_taxonomy_listing">';
            $response .= '<ul ' . $class . '>';
            foreach( $term as $terms ) {
              $response .= '<li><a href="' . get_term_link( $terms->term_id ) . '">' . $terms->name.( $atts['count'] == 1 ? '(' . $terms->count . ')' : '') . '</a>';
              if ( isset( $atts['specific_parent'] ) ? ( $terms->parent != 0 ) : ( $terms->parent == 0 ) ) {
                $response .= '<ul>';
                $arg_inner = array( 'hide_empty' => $atts['hide_empty'], 'order' => $order_attr, 'parent' => $terms->term_id );
                $child_terms = get_terms( $taxonomy_names_attr, $arg_inner ); 
                foreach( $child_terms as $child_term ) {
                  $response .= '<li><a href="' . get_term_link( $child_term->term_id ) . '">' . $child_term->name.( $atts['count'] == 1 ? '(' . $child_term->count . ')' : '') . '</a>';
                  if ( $child_term->parent != 0 ) {
                    $response .= '<ul>';
                    $arg_inner_child = array( 'hide_empty' => $atts['hide_empty'], 'order' => $order_attr, 'parent' => $child_term->term_id );
                    $inner_child_terms = get_terms( $taxonomy_names_attr, $arg_inner_child ); 
                    foreach( $inner_child_terms as $inner_child_term ) {
                      $response .= '<li><a href="' . get_term_link( $inner_child_term->term_id ) . '">' . $inner_child_term->name.( $atts['count'] == 1 ? '(' . $inner_child_term->count . ')' : '') . '</a></li>';
                    }
                    $response .= '</ul>';
                  }
                }
                $response .= '</ul></li>';
              }
            }
            $response .= '</ul>';
            return $response;
          }
          else {
            $response = '';
            $response = '<ul>';
            foreach ($term as $terms ){
              $response .= '<li><a href="' . get_term_link( $terms->term_id ) . '">' . $terms->name . ( $atts['count'] == 1 ? '(' . $terms->count . ')' : '') . '</a></li>';
            }
            $response .= '</ul>';
            $response .= '</div>';
            return $response;
          }
        }
      }
    }
    // Instantiate our class
    $taxonomy_listing_obj = new Taxonomy_Term_Addon();  
    vc_add_shortcode_param( 'taxonomy_names',  array($taxonomy_listing_obj, 'taxonomy_name_settings_field' ) );
    vc_add_shortcode_param( 'count_display', array( $taxonomy_listing_obj, 'count_display_settings_field' ) );
    vc_add_shortcode_param( 'hide_empty', array( $taxonomy_listing_obj, 'hide_empty_settings_field' ) );
    vc_add_shortcode_param( 'specific_parent', array( $taxonomy_listing_obj, 'specific_parent_settings_field' ) );
    vc_add_shortcode_param( 'top_level', array( $taxonomy_listing_obj, 'top_level_settings_field' ) );
    add_shortcode( 'taxonomy_term', array( $taxonomy_listing_obj, 'term_listing_func' ) );
    
    // Ajax call for selection of parent term id. 
    add_action( 'wp_ajax_get_taxonomy_term_id', 'get_taxonomy_term_id' );
    add_action( 'wp_ajax_nopriv_get_taxonomy_term_id', 'get_taxonomy_term_id' );
    function get_taxonomy_term_id() {
      global $wpdb;
      if ( isset( $_POST['postdata'] ) ) {
        $tax_name = sanitize_text_field( $_POST['postdata'] );
      }
      elseif ( isset( $_POST['postdata_selected'] ) ) {
        $tax_name = sanitize_text_field( $_POST['postdata_selected'] );
        $term_val = sanitize_text_field( $_POST['postdata_termselected'] );
      }
      $str="";
      if( ! empty( $tax_name ) ) {
        $arg = array( 'taxonomy' => $tax_name );
        $terms = get_categories( $arg );
        if ( isset( $_POST['postdata'] ) || isset( $_POST['postdata_termselected'] ) ) {
          $str .= '<option value="">Select Term</option>';
        }
        foreach( $terms as $term ) {
          if ( $term->parent == 0 ) {
            $str .= '<option value="' . $term->term_id . '" ' . ( $term->term_id == $term_val ? selected : '' ) . '>' . $term->name . '</option>';
          }
        }
      }
      echo $str;
      exit();
    }
Viewing 1 replies (of 1 total)
  • Plugin Author Manisha Makhija

    (@manishamakhija)

    Hi iwtravelllc,

    Just clearing your confusion about subcategory issue:

    ‘Include Subcategory’ and ‘Enter Parent term Id’ are not connected to each other.

    -> ‘Include Subcategory’ (checked) displays hierarchy of categories and sub categories of selected taxonomy.

    -> ‘Enter Parent term Id’ displays hierarchy of only subcategories(upto 3 level) of selected term.

Viewing 1 replies (of 1 total)
  • The topic ‘“Include Subcategory” unchecked still includes subcategories’ is closed to new replies.