Archived topic
How to add a prefix to the Category Title for certain parent category ?
6 replies · Started by Budi on January 27, 2021
Hello,
I want to add prefix for subcategory which has parent category name=xxx or id=xxx
I tried below code, but not worked.
add_filter( 'get_the_archive_title', function( $title ) {
$category = get_the_category();
$category_parent_id = $category[0]->category_parent;
if ( $category_parent_id = 10 ) {
$title = 'Prefix '. $title;
}
return $title;
}, 50 );
Thanks.
Hi there,
You can try this instead:
add_filter( 'get_the_archive_title', function( $title ) {
$archive_cat = get_queried_object();
if ($archive_cat->category_parent == 83) {
$title = 'Prefix '. $title;
}
return $title;
}, 50 );
Thanks, it works perfectly.
Can you explain about the value 50 ?
In some answers, I saw the value is 20.
Hello,
I also want to add a prefix to the Tag Title on tag archive.
What code should I insert for below code ?
add_filter( 'get_the_archive_title', function( $title ) {
$archive_cat = get_queried_object();
if ($archive_cat->category_parent == 83) {
$title = 'Prefix '. $title;
}
return $title;
}, 50 );
Thanks.
Try this:
add_filter( 'get_the_archive_title', function( $title ) {
$archive_cat = get_queried_object();
$tags = get_the_tags(get_queried_object()->ID);
if ($archive_cat->category_parent == 83) {
$title = 'Prefix '. $title;
}
if ($tags) {
foreach($tags as $tag) {
if ($tag->slug == 'test') {
$title = 'Prefix '. $title;
}
}
}
return $title;
}, 50 );
The prefix I want to add for the tag title is in tags archives, not tags in category archives.
Url example: http://www.generatepress.com/tag/flower
The code should be almost identical to the original code that worked. The only difference I can see is you'd use parent instead of category_parent: https://dev.to/ko31/return-value-of-getqueriedobject-in-wordpress-1h7n
These kinds of general WordPress coding questions are far better suited for a site like this: https://wordpress.stackexchange.com/
Thanks!