• Resolved caseyctg

    (@caseyctg)


    I’ve written a custom walker for my wordpress menu widget. It styles things for bootstrap. I’m trying to add a count next to each category item. I have that working, however I’ve converted to using a custom taxonomy so that the site is easier to use for my users.

    Before, I was finding the category count with this function.

    //this function adds category counts if the nav menu item is a category
    add_filter(‘the_title’, ‘wpse165333_the_title’, 10, 2);
    function wpse165333_the_title($title, $post_ID)
    {

    if( ‘nav_menu_item’ == get_post_type($post_ID) )
    {

    if( ‘taxonomy’ == get_post_meta($post_ID, ‘_menu_item_type’, true) && ‘careerstax’ == get_post_meta($post_ID, ‘_menu_item_object’, true) )
    {
    $category = get_category( get_post_meta($post_ID, ‘_menu_item_object_id’, true) );

    if(isset($category->count) && $category->count !=”){
    $cat_count_display = “<span class=’badge pull-right’>”.$category->count.”</span>”;
    $title .= sprintf(‘ %s’, $cat_count_display);
    }

    }
    }
    return $title;
    }

    I am looking to do the exact same thing but with a custom taxonomy. Does anyone have an idea of how I would do this? I’ve been struggling with this all day and though I would check the forum.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter caseyctg

    (@caseyctg)

    Ok, I used a different method. Rather than try to do this the hard way, I used this code in my sidebar php file. ‘careerstax’ is my taxonomy and ‘career-listings’ is my parent category for the ‘careers’ post_type.

    Hope this helps someone else.

    $args = array( 'hide_empty=0','orderby=count' );
    	$terms = get_terms( 'careerstax', $args );
    
    	if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
    		$count = count( $terms );
    
    		$parenttermtogetid = get_term_by( 'slug',  'career-listings',  'careerstax'  );
            $term_id = $parenttermtogetid->term_id;
    		$taxonomy_name = 'careerstax';
    		$termchildren = get_term_children( $term_id, $taxonomy_name );
    		$term_list="";
    
    		foreach ( $termchildren as $child ) {
    
    			$term = get_term_by( 'id', $child, $taxonomy_name );
    
    			 $args = array(
    			  'post_type'     => 'careers', //post type, I used 'product'
    			  'post_status'   => 'publish', // just tried to find all published post
    			  'posts_per_page' => -1,  //show all
    			  'tax_query' => array(
    				'relation' => 'AND',
    				array(
    				  'taxonomy' => 'careerstax',  //taxonomy name  here, I used 'product_cat'
    				  'field' => 'name',
    				  'terms' => array( $term->name )
    				)
    			  )
    			);
    
    			$query = new WP_Query( $args );
    
    			$termcount = (int)$query->post_count;
    
    			$term_list .= '<li class="list-group-item"><a href="' .
    			get_term_link( $term ) . '">' .
    			$term->name . '<span class="badge pull-right">'.
    			$termcount.'</span></a></li>';	 
    
    		}
    		echo '<div class="sideblock"><h3>Available Jobs by Department</h3><ul style="list-style-type:none;" id="menu-careersmenu" class="list-group">';
    		echo '<li class="list-group-item"><a href="../careerstaxonomy/career-listings/">All Career Postings </a></li>';
    		echo $term_list;
    		echo '</ul></div>';
    
    	}
    Thread Starter caseyctg

    (@caseyctg)

    I also then put this into a widget to be used.

    // Creating the widget
    class careers_dept_list_widget extends WP_Widget {
    
    function __construct() {
    parent::__construct(
    // Base ID of your widget
    'careers_dept_list_widget', 
    
    // Widget name will appear in UI
    __('Careers Listings Widget', 'careers_widget'), 
    
    // Widget description
    array( 'description' => __( 'Career Department Listings, this widget pulls from the career-listings top level career category.', 'careers_widget' ), )
    );
    }
    
    // Creating widget front-end
    // This is where the action happens
    public function widget( $args, $instance ) {
    $title = apply_filters( 'widget_title', $instance['title'] );
    
    // This is where you run the code and display the output
    
    $args = array( 'hide_empty=0','orderby=count' );
    	$terms = get_terms( 'careerstax', $args );
    
    	if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
    		$count = count( $terms );
    
    		$parenttermtogetid = get_term_by( 'slug',  'career-listings',  'careerstax'  );
            $term_id = $parenttermtogetid->term_id;
    		$taxonomy_name = 'careerstax';
    		$termchildren = get_term_children( $term_id, $taxonomy_name );
    		$term_list="";
    
    		foreach ( $termchildren as $child ) {
    
    			$term = get_term_by( 'id', $child, $taxonomy_name );
    
    			 $args = array(
    			  'post_type'     => 'careers', //post type, I used 'product'
    			  'post_status'   => 'publish', // just tried to find all published post
    			  'posts_per_page' => -1,  //show all
    			  'tax_query' => array(
    				'relation' => 'AND',
    				array(
    				  'taxonomy' => 'careerstax',  //taxonomy name  here, I used 'product_cat'
    				  'field' => 'name',
    				  'terms' => array( $term->name )
    				)
    			  )
    			);
    
    			$query = new WP_Query( $args );
    
    			$termcount = (int)$query->post_count;
    
    			$term_list .= '<li class="list-group-item"><a href="' .
    			get_term_link( $term ) . '">' .
    			$term->name . '<span class="badge pull-right">'.
    			$termcount.'</span></a></li>';	 
    
    		}
    		echo '<div class="sideblock"><h3>'.$title.'</h3><ul style="list-style-type:none;" id="menu-careersmenu" class="list-group">';
    		echo '<li class="list-group-item"><a href="../careerstaxonomy/career-listings/">All Career Postings </a></li>';
    		echo $term_list;
    		echo '</ul></div>';
    
    	}
    
    }
    
    // Widget Backend
    public function form( $instance ) {
    if ( isset( $instance[ 'title' ] ) ) {
    $title = $instance[ 'title' ];
    }
    else {
    $title = __( 'New title', 'careers_widget' );
    }
    // Widget admin form
    ?>
    <p>
    <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
    <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
    </p>
    <?php
    }
    
    // Updating widget replacing old instances with new
    public function update( $new_instance, $old_instance ) {
    $instance = array();
    $instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
    return $instance;
    }
    } // Class ends here
    
    // Register and load the widget
    function careers_load_widget() {
    	register_widget( 'careers_dept_list_widget' );
    }
    add_action( 'widgets_init', 'careers_load_widget' );

    Thread Starter caseyctg

    (@caseyctg)

    Closed

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to count custom taxonomy posts in wordpress walker’ is closed to new replies.