[Resolved] Can't get custom category titles to work

Home Forums Support Can't get custom category titles to work

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #952485
    Aäron

    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

    #953085
    Tom
    Lead Developer
    Lead Developer

    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 🙂

    #953150
    Aäron

    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

    #953340
    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;
    }
    #953409
    Tom
    Lead Developer
    Lead Developer

    Awesome, glad you got it working 🙂

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.