Advertisement
arie_cristianD

jnews_get_primary_category

Oct 29th, 2023
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.62 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.         $cat      = get_term( $category );
  7.  
  8.         if ( ! empty( $category ) && isset( $cat ) ) {
  9.             $category_id = $category;
  10.         } else {
  11.             $categories = array_slice( get_the_category( $post_id ), 0, 1 );
  12.             if ( empty( $categories ) ) {
  13.                 return null;
  14.             }
  15.             $category    = array_shift( $categories );
  16.             $category_id = $category->term_id;
  17.         }
  18.     }
  19.  
  20.     return apply_filters( 'jnews_primary_category', $category_id, $post_id );
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement