Site logo

Archived topic

Putting blog post category on top

5 replies · Started by Margot on September 7, 2018

Viewing posts 1–6 of 6

On my blog posts, I want to put the post category above/before the post title. Is there a way I can do this?

Hi there,

Are you wanting to do this on the blog page as well as single posts?

Let me know :)

Hi Leo, I'm looking to do this only on single posts. Thanks!

Try this PHP snippet here:

add_filter( 'generate_category_list_output','lh_remove_singlecategories' );
function lh_remove_singlecategories( $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 :)

Is it possible to style separately only the Category text? As a black slightly rounded box with white text? The rest of the meta content (author, date) should not be affected.

Cheers!

Can you open a new topic and link us to the page in question?

Thanks :)

This archived topic is closed to new replies.