Site logo

Archived topic

Category Meta Tag Gone

6 replies · Started by Rey on May 8, 2021

Viewing posts 1–7 of 7

I've been trying to move the location of the category meta tag to generate_before_entry_title. So, here's what I did.
1. Removed categories from default position using the following code:

// Remove categories from default position
add_filter( 'generate_category_list_output','tu_remove_categories' );
function tu_remove_categories( $categories ) {
	if ( is_home() || is_archive() ) {
		return '';
	}
	
	return $categories;
}

The problem is this:
When I removed that code now, the category meta tag is gone. It is enabled in customize settings.
https://prnt.sc/12o81e5

Nevermind. The above issue is now solved. There was a bug in my code when I tried to remove the commas. But here's my issue now. When I tried to use the ff code, it doesn't work.

// Add category link above entry title
add_action( 'generate_before_entry_title','tu_cats_before_title', 15 );
function tu_cats_before_title() {
	if ( is_home() || is_archive() ) {
		$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></div>',
				_x( 'Categories', 'Used before category names.', 'generatepress' ),
				$categories_list
			);
		}
	}
}

Here's my staging site:
https://app-60799954c1ac183a8cdca6fa.closte.com/

This one is solved already with Tom's post here

https://generatepress.com/forums/topic/change-category-links-style-position/#post-480156

Just one last issue:

I want to remove the comma as seen here: https://prnt.sc/12o9usi

This code DIDN'T work

// Remove comma between category
add_filter( 'generate_category_list_output', 'tu_remove_category_commas' );
function tu_remove_category_commas() {
	$categories_list = get_the_category_list( ' ' );
	return sprintf( '<span class="cat-links"><span class="screen-reader-text">%1$s </span>%2$s</span>', // WPCS: XSS ok, sanitization ok.
		esc_html_x( 'Categories', 'Used before category names.', 'generatepress' ),
		$categories_list
	);
}

Here's the entire code that I used:

add_filter( 'generate_category_list_output','tu_remove_categories' );
function tu_remove_categories( $categories ) {
	if ( is_home() || is_archive() ) {
		return '';
	}
	
	return $categories;
}

add_action( 'generate_before_entry_title','tu_cats_above_title' );
function tu_cats_above_title() {
	if ( is_home() || is_archive() ) {
		$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
			);
		}
	}
}

// Remove comma between category
add_filter( 'generate_category_list_output', 'tu_remove_category_commas' );
function tu_remove_category_commas() {
	$categories_list = get_the_category_list( ' ' );
	return sprintf( '<span class="cat-links"><span class="screen-reader-text">%1$s </span>%2$s</span>', // WPCS: XSS ok, sanitization ok.
		esc_html_x( 'Categories', 'Used before category names.', 'generatepress' ),
		$categories_list
	);
}

Comma is still there.

Hi there,

You can remove the comma by replacing this:

_x( ', ', 'Used between list items, there is a space after the comma.', 'generatepress' )

With this:

''

Also, you can achieve this with no PHP at all by disabling the default category post meta and using a Post Meta Template: https://docs.generatepress.com/article/block-element-post-meta-template/

Hope this helps!

You're the best, Sir. Thanks much!

Glad I could help! :)

This archived topic is closed to new replies.