arie_cristianD

adding category bage before post title (single post)

Mar 8th, 2026
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.60 KB | None | 0 0
  1. add_action(
  2.     'jnews_single_post_before_title',
  3.     function ( $post_id ) {
  4.         if ( function_exists( 'jnews_get_primary_category' ) ) {
  5.             $cat_id   = jnews_get_primary_category( $post_id );
  6.             $category = '';
  7.  
  8.             if ( $cat_id ) {
  9.                 $category = get_category( $cat_id );
  10.                 if ( $category && ( isset( $category->slug ) && isset( $category->name ) ) ) {
  11.                     $class    = 'class="category-' . $category->slug . '"';
  12.                     $category = '<div class="custom-category-badge"><a href="' . get_category_link( $cat_id ) . "\" {$class}>" . $category->name . '</a></div>';
  13.                 }
  14.                 echo $category;
  15.             }
  16.         }
  17.     }
  18. );
Advertisement
Add Comment
Please, Sign In to add comment