[Resolved] Display post categories above title

Home Forums Support Display post categories above title

This topic contains 5 replies, has 3 voices, and was last updated by  Leo 2 months ago.

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #645381

    Luong

    Hello,

    How can I display the category meta on a single post above the title instead as it is below the content?

    Thanks for your help!

    #645410

    Leo Customer Support

    Hi there,

    Try this snippet:

    add_filter( 'generate_category_list_output','lh_remove_categories' );
    function lh_remove_categories( $categories ) {
    	if ( is_single() ) {
    		return '';
    	}
    	
    	return $categories;
    }
    
    add_action( 'generate_before_entry_title','lh_single_cats_above_title' );
    function lh_single_cats_above_title() {
    	if ( is_single() ) {
    		$categories_list = get_the_category_list( _x( ', ', 'Used between list items, there is a space after the comma.', 'generatepress' ) );
    		if ( $categories_list ) {
    			printf( '<span class="entry-meta cat-links"><span class="screen-reader-text">%1$s </span>%2$s</span>',
    				_x( 'Categories', 'Used before category names.', 'generatepress' ),
    				$categories_list
    			);
    		}
    	}
    }

    Adding PHP: https://docs.generatepress.com/article/adding-php/

    Let me know 🙂

    #645531

    Luong

    Thank you a lot, it works!

    #645752

    Leo Customer Support
    #701113

    andrew

    Hi,
    I used this code:

    add_filter( 'generate_category_list_output','lh_remove_categories' );
    function tu_remove_categories( $categories ) {
    	if ( is_single() ) {
    		return '';
    	}
      if ( is_home() ) {
    		return '';
    	}
    	
    	return $categories;
    }
    
    add_action( 'generate_before_entry_title','lh_single_cats_above_title' );
    function lh_single_cats_above_title() {
    	if ( is_single() ) {
    		$categories_list = get_the_category_list( _x( ', ', 'Used between list items, there is a space after the comma.', 'generatepress' ) );
    		if ( $categories_list ) {
    			printf( '<span class="entry-meta cat-links"><span class="screen-reader-text">%1$s </span>%2$s</span>',
    				_x( 'Categories', 'Used before category names.', 'generatepress' ),
    				$categories_list
    			);
    		}
    	}
      if ( is_home() ) {
    		$categories_list = get_the_category_list( _x( ', ', 'Used between list items, there is a space after the comma.', 'generatepress' ) );
    		if ( $categories_list ) {
    			printf( '<span class="entry-meta cat-links"><span class="screen-reader-text">%1$s </span>%2$s</span>',
    				_x( 'Categories', 'Used before category names.', 'generatepress' ),
    				$categories_list
    			);
    		}
    	}
    }

    But my error_log_php into cpanel display shows me a lot of error:
    PHP Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'lh_remove_categories' not found or invalid function name in /home/****/public_html/wp-includes/class-wp-hook.php on line 286

    What’s wrong and how do I fix it?

    Thank you very much

    #701493

    Leo Customer Support

    Can you open a new topic for your question?

    Thanks!

Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘Display post categories above title’ is closed to new replies.