pustakakoran

WP-Snips: Menambahkan Jumlah Post di Samping Menu

Oct 21st, 2017
79
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Cara Pakai:
  2. 1. Paste salah satu kode berikut di function.php, yang pertama simpel.
  3. 2. Bikin Custom menu. Tanpa custom menu tidak muncul menu post count-nya.
  4.  
  5. Sumber: https://wordpress.stackexchange.com/questions/165333/how-to-add-category-post-count-in-main-navigation-menu
  6.  
  7. add_filter('the_title', 'wpse165333_the_title', 10, 2);
  8. function wpse165333_the_title($title, $post_ID)
  9. {
  10. if( 'nav_menu_item' == get_post_type($post_ID) )
  11. {
  12. if( 'taxonomy' == get_post_meta($post_ID, '_menu_item_type', true) && 'category' == get_post_meta($post_ID, '_menu_item_object', true) )
  13. {
  14. $category = get_category( get_post_meta($post_ID, '_menu_item_object_id', true) );
  15. $title .= sprintf(' (%d)', $category->count);
  16. }
  17. }
  18. return $title;
  19. }
  20.  
  21.  
  22.  
  23.  
  24.  
  25. Sumber: https://wordpress.stackexchange.com/questions/229767/show-posts-count-for-categories-and-tags-in-wp-nav-menu
  26.  
  27. function ggstyle_menu_item_count( $output, $item, $depth, $args ) {
  28. // Check if the item is a Category or Custom Taxonomy
  29. if( $item->type == 'taxonomy' ) {
  30. $object = get_term($item->object_id, $item->object);
  31.  
  32. // Check count, if more than 0 display count
  33. if($object->count > 0)
  34. $output .= "<span class='menu-item-count'> (".$object->count.")</span>";
  35. }
  36.  
  37. return $output;
  38. }
  39. add_action( 'walker_nav_menu_start_el', 'ggstyle_menu_item_count', 10, 4 );
RAW Paste Data