fauzanjeg

JKIT || Show Primary Category for All Posts on Post Block widget

Apr 6th, 2022 (edited)
418
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.54 KB | None | 0 0
  1. /** Show Primary Category for All Posts on Post Block widget */
  2. function jkit_show_primary_category_on_post_block_for_all_post( $category_id, $post_id ) {
  3.     if ( ! empty( $category_id ) ) {
  4.         return $category_id;
  5.     }
  6.  
  7.     $categories = array_slice( get_the_category( $post_id ), 0, 1 );
  8.  
  9.     if ( empty( $categories ) ) {
  10.         return null;
  11.     }
  12.  
  13.     $category    = array_shift( $categories );
  14.     $category_id = $category->term_id;
  15.  
  16.     return $category_id;
  17. }
  18. add_filter( 'jeg_primary_category', 'jkit_show_primary_category_on_post_block_for_all_post', 10, 2 );
  19.  
Add Comment
Please, Sign In to add comment