Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function jnews_get_primary_category( $post_id ) {
- $category_id = null;
- if ( get_post_type( $post_id ) === 'post' ) {
- $category = vp_metabox( 'jnews_primary_category.id', null, $post_id );
- if ( ! empty( $category ) ) {
- $category_id = $category;
- } else {
- $categories = get_the_category( $post_id );
- $primary_categories = array();
- $exclude_categories = array( 30, 31, 2, 29 ,4 ); /* change this value with all categories id you want exclude from primary category*/
- foreach ( $categories as $key => $category ) {
- if ( ! in_array( $category->cat_ID, $exclude_categories ) ) {
- array_push( $primary_categories, $category );
- }
- }
- $categories = ( ! empty( $primary_categories ) ) ? $primary_categories : $categories;
- $categories = array_slice( $categories, 0, 1 );
- if ( empty( $categories ) ) {
- return null;
- }
- $category = array_shift( $categories );
- $category_id = $category->term_id;
- }
- }
- return apply_filters( 'jnews_primary_category', $category_id, $post_id );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement