WordPress.org

Forums

Category name in page title - Google Analytics content grouping (4 posts)

  1. Honza Felt
    Member
    Posted 1 year ago #

    Hello,

    Google has launched a new Content Grouping feature. There is just one issue with that.

    URLs on my blog are structured in the following way: http://www.mydomain.eu/post-name. I do not want to add category name to the URL as it would impact its robot-friendliness.

    Fortunately, GA also allows to group content by page title. So if I can put the names of the categories into the <title></title> tag, I still can create the content group.

    Now how does that work? Is there a way to add category into the <title></title> without having to input it manually to every single post?

    I run a couple of sites. Is there a way of universally fixing this for all of them? I know an All-in-One SEO plug-in would solve this but I don't like to install additional plug-ins unless I really have to.

    Cheers for ideas!

  2. Mrinal Kanti Roy
    Member
    Posted 1 year ago #

    Hi

    Add the following codes in your website's activated Theme header.php file.

    <title>
    	<?php wp_title(''); ?><?php if(wp_title('', false)) { echo ' –'; } ?>
    <?php if(is_single()) { $cats = get_the_category($post->ID);
    foreach( $cats as $cat ) { echo $cat->cat_name; echo ' - '; }
    } ?>
    	<?php bloginfo('name'); ?>
    </title>

    Reference: Function Reference/get the category

  3. Honza Felt
    Member
    Posted 1 year ago #

    Thank you so much, Mrinal! It works!

    Just a question, if I wanted to add two categories into the title, would that be possible? My posts are very rarely only in one category.

    Thank you for your advice!

  4. Mrinal Kanti Roy
    Member
    Posted 1 year ago #

    Hi,

    I've checked the following snippet & worked for me with Twenty Twelve Theme. You can check also, I just call all the categories instead of a single category.

    <title>
    	<?php wp_title(''); ?><?php if(wp_title('', false)) { echo ' –'; } ?>
    <?php if(is_single()) { $cats = get_categories($cat_args);
    foreach( $cats as $cat ) { echo $cat->cat_name; echo ' - '; }
    } ?>
    	<?php bloginfo('name'); ?>
    </title>

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.