Archived topic
Comma between category modify existing snippet
11 replies · Started by Matthew on February 14, 2021
Got three different snippets that custom position the categories and tags for single posts and archives, for both my regular posts and custom posts type. Can I put one below to start, and I am wanting to delete the comma between the categories. Thanks. I'll try to then change other snippets myself.
First snippet:
Displays the categories above the title on single posts of my CPT.
Displays the categories above the title on home page and archives of regular posts.
I wanted to have a crack but concerned about stuffing the snippet up and what happens does it prevent this from happening i.e. safeguard
add_filter( 'generate_category_list_output','tu_remove_categories' );
function tu_remove_categories( $categories ) {
if ( is_home() || is_archive() || is_post_type_archive( 'pictures' ) || is_singular( 'pictures' ) ) {
return '';
}
return $categories;
}
add_action( 'generate_before_content','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
);
}
}
if ( 'pictures' === get_post_type() ) {
$genres_list = get_the_term_list( get_the_ID(), 'genres', '', ', ' );
if ( $genres_list ) {
printf(
'<span class="cat-links">%3$s<span class="screen-reader-text">%1$s </span>%2$s</span>',
esc_html_x( 'Genres', 'Used before cat names.', 'generatepress' ),
$genres_list,
apply_filters( 'generate_inside_post_meta_item_output', '', 'genres' )
);
}
}
}
Hi there,
There's no need to do any changes with these PHP snippets.
You can change the term separator using the filter generate_term_separator.
Example: changing the default separator , to '-'
add_filter( 'generate_term_separator', function() {
return '-';
} );
You can even replace - with any character you want or simply, an empty space like this:
add_filter( 'generate_term_separator', function() {
return ' ';
} );
Hi Elvin,
I added this in my Snippets plugin but it hasn't worked.
Could any of my other snippets I mentioned be overriding it?
Ah yes that's right.
I've re-read your code and you have this line: $genres_list = get_the_term_list( get_the_ID(), 'genres', '', ', ' );
https://developer.wordpress.org/reference/functions/get_the_term_list/
This line sets the separator to ,. We can change this line to this:
$genres_list = get_the_term_list( get_the_ID(), 'genres', '', ' ' ); so the comma is completely removed.
That worked and I repeated what you said with my other snippets to do all (commas removal on single and archives) but one. It's for regular posts on the archives, still a comma between categories. Also, there is a ticket icon before the tags I want to remove, pls see below capture:
https://drive.google.com/file/d/14vppjsHOUTHvXNAy2ULUmaGMrlRzV4Rs/view?usp=sharing
Here are other two snippet:
add_action( 'generate_post_meta_items', function( $item ) {
if ( 'words' === $item ) {
$words_list = get_the_term_list( get_the_ID(), 'words', '', ' ' );
if ( $words_list ) {
printf(
'<span class="tags-links">%3$s<span class="screen-reader-text">%1$s </span>%2$s</span> ',
esc_html_x( 'Words', 'Used before tag names.', 'generatepress' ),
$words_list,
apply_filters( 'generate_inside_post_meta_item_output', '', 'words' )
);
}
}
} );
add_filter( 'generate_footer_entry_meta_items', function( $items ) {
if ( 'pictures' === get_post_type() ) {
return array(
'genres',
'words',
'comments-link',
);
}
return $items;
} );
add_filter( 'generate_footer_meta_post_types', function( $types ) {
$types[] = 'pictures';
return $types;
} );
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
);
}
}
}
Note: I did try to solve this and removed a wrong comma and it whipped my ass (but the system did a glove save beauty)!
For the tag icon:
You can try filtering it out using this code:
add_filter( 'generate_tag_list_output', function( $output ) {
$tags_list = get_the_tag_list( '', '' );
return sprintf( '<span class="tags-links"><span class="screen-reader-text">%1$s </span>%2$s</span>', // WPCS: XSS ok, sanitization ok.
esc_html_x( 'Tags', 'Used before tag names.', 'generatepress' ),
$tags_list
);
} );
As for the other commas, can you link us to the site in question so I can check how that comma is being added?
It's strange that is still there, even when its removed in get_the_category_list( _x( ' ', 'Used between list items, there is a space after the comma.', 'generatepress' ) );
Filter works but there is a new problem that is no more space between each separate tag now.
For the other thing (the commas) I have sent my site domain and password using your account issue form. Please check there
Thanks
Filter works but there is a new problem that is no more space between each separate tag now.
You can edit the line code from the filter from this:
$tags_list = get_the_tag_list( '', '' );
To this: $tags_list = get_the_tag_list( '', ' ', '' );
For the other thing (the commas) I have sent my site domain and password using your account issue form. Please check there
I've checked the site but I wasn't able to find any archive of posts with categories that have commas.
Can you link us to a specific page where you've seen it? You can use the private information text field to link it to us so you don't have to mail it in. :)
Can we refer to the link I put earlier:
https://drive.google.com/file/d/14vppjsHOUTHvXNAy2ULUmaGMrlRzV4Rs/view?usp=sharing
It's on my home page at the bottom. The comma between 'News' and 'Uncategorised'.
The change to the filter worked
On your code here: https://generatepress.com/forums/topic/comma-between-category-modify-existing-snippet/#post-1658519
You have this line within the tu_cats_above_title() function.
$categories_list = get_the_category_list( _x( ', ', 'Used between list items, there is a space after the comma.', 'generatepress' ) );
Can you change the line of code to this?
$categories_list = get_the_category_list( _x( ' ', 'Used between list items, there is a space after the comma.', 'generatepress' ) );
Let us know how it goes.
It's very excellent now well done
Nice one. Glad you got it sorted. :D