Advertisement
fauzanjeg

Epic News Elements || Primary Category Filter

Feb 25th, 2025
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.10 KB | None | 0 0
  1. /**
  2.  * Epic News Elements || Primary Category Filter
  3.  */
  4. if ( ! function_exists( 'custom_epic_primary_category' ) ) {
  5.     /**
  6.      * Custom Filter for Epic Primary Category
  7.      *
  8.      * @param int      $category_id Category ID.
  9.      * @param null|int $post_id Post ID.
  10.      *
  11.      * @return int
  12.      */
  13.     function custom_epic_primary_category( $category_id, $post_id, $recursive = false ) {
  14.         if ( $recursive ) {
  15.             $check_category = get_category( $category_id );
  16.  
  17.             if ( $check_category->parent === 0 ) {
  18.                 return $check_category->term_id;
  19.             }
  20.  
  21.             return custom_epic_primary_category( $category->parent, $post_id, true );
  22.         }
  23.  
  24.         $categories            = get_the_category( $post_id );
  25.         $primary_cateories_ids = array();
  26.  
  27.         foreach ( $categories as $category ) {
  28.             if ( $category->parent === 0 ) {
  29.                 $primary_cateories_ids[] = $category->term_id;
  30.             } else {
  31.                 $primary_cateories_ids[] = custom_epic_primary_category( $category->parent, $post_id, true );
  32.             }
  33.         }
  34.  
  35.         return array_shift( $primary_cateories_ids );
  36.     }
  37.  
  38.     add_filter( 'epic_primary_category', 'custom_epic_primary_category', 10, 2 );
  39. }
  40.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement