Site logo

Archived topic

Can't get custom category titles to work

4 replies · Started by Aäron on July 8, 2019

Viewing posts 1–5 of 5

Hi GeneratePress,

Firstly, thanks for the great theme! With just a few plugins I get consistent 100/100 scores on Google Pagespeed Insights! :)

I'm in the process further of optimizing my website for search engines before I will start posting more content. So far I have succeeded in altering the URL, and page titles to my wish, but not the category page titles. I would like the category page titles to also include the parent category title. What am I doing wrong? I have the latest version of GP Theme and GP Premium, I have tried disabling all my plugins, clearing caches, tried all kinds of hooks, but all to no avail.

This is the code in functions.php that I think should work (it works for a different hook on the HTML-page-titles), but could you please also have a look at it?

add_filter( 'get_the_archive_title' ,'custom_category_page_title' );
function custom_category_page_title ($title) {
	if( is_category() ) {
		$current_category = get_queried_object();
		$current_category_id = $current_category->term_id;
		$categories = get_category_parents($current_category_id, 'false', ' ');
		$title = $categories;
    }
    return $title;
}

Kind regards,

Aäron

Hi there,

What about this?:

add_filter( 'get_the_archive_title', function( $title ) {
    if ( is_category() ) {
        $cat = get_the_category();
        $parentCatName = get_cat_name($cat[0]->parent);

        if ( ! empty( $parentCatName ) ) {
            $title = $parentCatName . ' : ' . $title;
        }
    }

    return $title;
} );

Let me know :)

Hi,

Thanks! Unfortunately it does not solve my problem. It concerns for example this page: https://itreport.info/gartner/market-guide/. I would like the title to be "Gartner Market Guide" instead of just "Market Guide". Any hints on where else I could look / make changes?

Kind regards, Aäron

Success! The problem was the priority in the add_filter: it was too low.

The working code:

add_filter( 'get_the_archive_title', 'filter_category_page_title', 100, 1);
function filter_category_page_title ($title) {
	if( is_category() ) {
		$current_category = get_queried_object();
		$current_category_id = $current_category->term_id;
		$categories = get_category_parents($current_category_id, 'false', ' ');
		$title = $categories;
    }
	return $title;
}

Awesome, glad you got it working :)

This archived topic is closed to new replies.