Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #937420

    Is there a way to show the category name in the header titles on single post pages, instead of just “Blog – Latest News” (see screen grab https://www.evernote.com/shard/s320/sh/eca13dc5-01d6-41ce-86bc-11d6887f62e1/21845d0cd306bca2343b768d22bd9b57 )?

    Thanks!

    #937950

    Hey sky19er,

    That should be your category – except if you are on blog page
    Can you share your web site link and access?

    Best regards,
    Basilis

    #937960

    Sure (credentials in PC) — I have those header titles hidden with css, for single post pages — they do work on Archive pages (e.g., https://www.waternewsnetwork.com/category/education/ or see screen grab https://www.evernote.com/shard/s320/sh/f56ed5ba-d7de-42b5-b9a9-fc766663abe6/51e8181ecd261d785ddcde3181c15aca ) — If I could just show “Category: Education” or just “Education” in that strip for all posts in the Education category, that’d be perfect.

    Thanks and let me know if you have any other questions!

    #938439

    Hi,

    I tried to login and it does not allow me.
    Can you check them again please?

    Best regards,
    Basilis

    #938693

    Just worked for me, but I have used these credentials for a few, recent tickets — maybe two of you were trying to log in at the same time…?

    #939592

    Hi,

    This is possible but you have to make sure that there is only one category per post. Add this filter in the functions.php file.

    add_filter('avf_title_args', 'avf_title_args_mod_category', 10, 2);
    function avf_title_args_mod_category($args, $id)
    {
    	$categories = get_the_terms(get_the_ID(), 'category');
    	foreach($categories as $category) {
    		$args['title'] = $category->name;
    	}
    
        return $args;
    }
    

    Best regards,
    Ismael

    #940193

    Nice, that’s a good step in the right direction! I added the h1 tag around it (see below), like the other, category header titles, so they look the same.

    But there’s no way to show the other categories? Posts are so often in more than one category. Sorry to press on this, but if a user is browsing one category, clicks on a post, and then is taken to a page with a different category title, that’d be pretty confusing, I think.

    Oh, also, this category just links to the home page — can we make it link to the archive page for that category?

    Thanks again!

    add_filter('avf_title_args', 'avf_title_args_mod_category', 10, 2);
    function avf_title_args_mod_category($args, $id)
    {
    	$categories = get_the_terms(get_the_ID(), 'category');
    	foreach($categories as $category) {
    		$args['title'] = '<h1 class="main-title entry-title">'.$category->name.'</h1>';
    	}
    
        return $args;
    }
    
    #940782

    Hi,

    The modification above will display a single category. If you want to add the link, please replace it with this.

    add_filter('avf_title_args', 'avf_title_args_mod_category', 10, 2);
    function avf_title_args_mod_category($args, $id)
    {
    	if( is_single() ) {
    		$categories = get_the_terms(get_the_ID(), 'category');
    		foreach($categories as $category) {
    			$category_link = get_term_link( $category );
    
    			if ( is_wp_error( $category_link ) ) {
    				continue;
    			}
    
    			$args['title'] = $category->name;
    			$args['link'] = esc_url( $category_link );
    			$args['heading'] = 'h1';
    		}
    	}
    	
        return $args;
    }
    
    

    Best regards,
    Ismael

    #941239

    Ooh, yeah, that’s much better — that handles the title styling properly, too, so I don’t have to modify that. But it’s too tricky to get multiple categories to show? And if it can only show one, how is it choosing that one — it doesn’t seem to be choosing the Primary Category — do you know how it chooses which category to use as the title?

    Thanks!

    #942385

    Hi,

    It will display the very last category in the list, usually ordered alphabetically. You can’t select a primary category by default. Did you install the Yoast plugin? These threads might help.

    // https://gist.github.com/jawinn/1b44bf4e62e114dc341cd7d7cd8dce4c
    // https://github.com/Yoast/wordpress-seo/issues/4038

    Best regards,
    Ismael

    #942732

    OK, thanks. We do have the Yoast plugin installed, but Integrating that code with what you gave me above is a little over my head, apparently (just tried it, to no avail). If you feel like showing me how that’d work, please do.. Otherwise, I’ll just hope my client can keep posts in just one category for now, and if not I’ll jump back into this.

    Thanks again!

    #943451

    Hi,

    According to the forum, you can use this code to fetch the primary category.

    $cat = new WPSEO_Primary_Term('category', get_the_ID());
    $cat = $cat->get_primary_term();
    
    

    Adjust the filter to this code then.

    add_filter('avf_title_args', 'avf_title_args_mod_category', 10, 2);
    function avf_title_args_mod_category($args, $id)
    {
    	if( is_single() ) {
    		$category = new WPSEO_Primary_Term('category', get_the_ID());
    		$category = $category->get_primary_term();
    
    		$category_link = get_term_link( $category );
    
    		$args['title'] = $category->name;
    		$args['link'] = esc_url( $category_link );
    		$args['heading'] = 'h1';
    	}
    
        return $args;
    }
    

    Best regards,
    Ismael

Viewing 12 posts - 1 through 12 (of 12 total)
  • The topic ‘show category name in header title’ is closed to new replies.