Advertisement
arie_cristianD

override function jnews_get_primary_category

Dec 13th, 2023 (edited)
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.04 KB | None | 0 0
  1. function jnews_get_primary_category( $post_id ) {
  2.     $category_id = null;
  3.  
  4.     if ( get_post_type( $post_id ) === 'post' ) {
  5.         $category = vp_metabox( 'jnews_primary_category.id', null, $post_id );
  6.  
  7.         if ( ! empty( $category ) ) {
  8.             $category_id = $category;
  9.         } else {
  10.             $categories         = get_the_category( $post_id );
  11.             $primary_categories = array();
  12.             $exclude_categories = array( 30, 31, 2, 29 ,4 ); /*  change this value with all categories id you want exclude from primary category*/
  13.             foreach ( $categories as $key => $category ) {
  14.                 if ( ! in_array( $category->cat_ID, $exclude_categories ) ) {
  15.                     array_push( $primary_categories, $category );
  16.                 }
  17.             }
  18.             $categories = ( ! empty( $primary_categories ) ) ? $primary_categories : $categories;
  19.             $categories = array_slice( $categories, 0, 1 );
  20.             if ( empty( $categories ) ) {
  21.                 return null;
  22.             }
  23.             $category    = array_shift( $categories );
  24.             $category_id = $category->term_id;
  25.         }
  26.     }
  27.     return apply_filters( 'jnews_primary_category', $category_id, $post_id );
  28. }
  29.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement