-
AuthorPosts
-
April 5, 2018 at 4:17 am #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!
April 6, 2018 at 12:03 am #937950Hey sky19er,
That should be your category – except if you are on blog page
Can you share your web site link and access?Best regards,
BasilisApril 6, 2018 at 12:24 am #937960Sure (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!
April 6, 2018 at 11:21 pm #938439Hi,
I tried to login and it does not allow me.
Can you check them again please?Best regards,
BasilisApril 8, 2018 at 2:08 am #938693Just 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…?
April 10, 2018 at 6:26 am #939592Hi,
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,
IsmaelApril 11, 2018 at 5:12 am #940193Nice, 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; }
April 12, 2018 at 5:43 am #940782Hi,
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,
IsmaelApril 13, 2018 at 5:02 am #941239Ooh, 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!
April 16, 2018 at 3:20 am #942385Hi,
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/4038Best regards,
IsmaelApril 16, 2018 at 7:44 pm #942732OK, 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!
April 18, 2018 at 7:19 am #943451Hi,
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 -
AuthorPosts
- The topic ‘show category name in header title’ is closed to new replies.