Site logo

Archived topic

How to add a prefix to the Category Title for certain parent category ?

6 replies · Started by Budi on January 27, 2021

Viewing posts 1–7 of 7

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 );
This archived topic is closed to new replies.