0

i have a taxonomy list with post count and it sorted by post count. but i want taxonomy list to show only curtain post type count and sort by ount of this post type this is my code

function __construct() {
    parent::__construct(
        'wp_categories_widget', // Base ID
        __( 'WP Categories list', 'mrwebsolution' ), // Name
        array( 'description' => esc_html__( 'Display categories list of all taxonomy post type', 'mrwebsolution' ), ) // Args
    );
    if(!is_admin())
    add_action('wcw_style',array($this,'wcw_style_func'));
    add_filter( "plugin_action_links_".plugin_basename( __FILE__ ), array(&$this,'wcw_add_settings_link') );
}

/**
 * Front-end display of widget.
 *
 * @see WP_Widget::widget()
 *
 * @param array $args     Widget arguments.
 * @param array $instance Saved values from database.
 */
public function widget( $args, $instance ) {
    echo $args['before_widget'];
    if ( ! empty( $instance['wcw_title'] ) && !$instance['wcw_hide_title']) {
        echo $args['before_title'] . apply_filters( 'widget_title', $instance['wcw_title'] ) . $args['after_title'];
    }
    // add css      
    do_action('wcw_style','wcw_style_func');
    /** return category list */
    if($instance['wcw_taxonomy_type']){
        $va_category_HTML ='<div class="ve-cat-widget-div">';
        $va_category_HTML .='<ul class="ve-cat-widget-listing">';
            $args_val = array( 'hide_empty=0' );                
            $excludeCat= $instance['wcw_selected_categories'] ? $instance['wcw_selected_categories'] : '';
            $wcw_action_on_cat= $instance['wcw_action_on_cat'] ? $instance['wcw_action_on_cat'] : '';
            if($excludeCat && $wcw_action_on_cat!='')
            $args_val[$wcw_action_on_cat] = $excludeCat;

            $terms = get_terms( $instance['wcw_taxonomy_type'], array('orderby' => 'count', 'order' => 'DESC','hide_empty'=>0 ));
            if ( $terms ) { 

                foreach ( $terms as $term ) {

                    $term_link = get_term_link( $term );

                    if ( is_wp_error( $term_link ) ) {
                    continue;
                    }

                $carrentActiveClass=''; 

                if($term->taxonomy=='category' && is_category())
                {
                  $thisCat = get_category(get_query_var('cat'),false);
                  if($thisCat->term_id == $term->term_id)
                    $carrentActiveClass='class="active-cat"';
                }

                if(is_tax())
                {
                    $currentTermType = get_query_var( 'taxonomy' );
                    $cat_icon = get_field('logo', $queried_object);
                    $termId= get_queried_object()->term_id;
                     if(is_tax($currentTermType) && $termId==$term->term_id)
                      $carrentActiveClass='class="active-cat"';
                }
                    $icon = get_field('logo', $term->taxonomy . '_' . $term->term_id); 
                    $va_category_HTML .= '<li '.$carrentActiveClass.'>' .sprintf('<img src="%s" />', $icon) . '</a>';
                    $va_category_HTML .='<a href="' . esc_url( $term_link ) . '">' . $term->name . '</a>';
                    if (empty( $instance['wcw_hide_count'] )) {
                    $va_category_HTML .='<span class="post-count">'.$term->count.'</span>';
                    }

                    $va_category_HTML .='</li>';
                }
            }
        $va_category_HTML .='</ul></div>';
        echo $va_category_HTML;
        }   
    echo $args['after_widget'];
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.