Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Epic News Elements || Primary Category Filter
- */
- if ( ! function_exists( 'custom_epic_primary_category' ) ) {
- /**
- * Custom Filter for Epic Primary Category
- *
- * @param int $category_id Category ID.
- * @param null|int $post_id Post ID.
- *
- * @return int
- */
- function custom_epic_primary_category( $category_id, $post_id, $recursive = false ) {
- if ( $recursive ) {
- $check_category = get_category( $category_id );
- if ( $check_category->parent === 0 ) {
- return $check_category->term_id;
- }
- return custom_epic_primary_category( $category->parent, $post_id, true );
- }
- $categories = get_the_category( $post_id );
- $primary_cateories_ids = array();
- foreach ( $categories as $category ) {
- if ( $category->parent === 0 ) {
- $primary_cateories_ids[] = $category->term_id;
- } else {
- $primary_cateories_ids[] = custom_epic_primary_category( $category->parent, $post_id, true );
- }
- }
- return array_shift( $primary_cateories_ids );
- }
- add_filter( 'epic_primary_category', 'custom_epic_primary_category', 10, 2 );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement