Tagged: Product, title, woocommerce
-
AuthorPosts
-
November 16, 2017 at 1:07 am #877301
I am using the hook and code to dynamically change the avia title tag and it works everywhere but for the <title></title> area in the page header area. Is this a bug or is there a different, specific bit of code to target that?
add_filter('avf_title_tag', 'avia_change_title_tag', 10, 2); function avia_change_title_tag($title, $wptitle) { if (is_product() && has_term( 'Business Cards', 'product_cat' )){ $title = the_title().' Business Cards | '. get_bloginfo('name'); } return $title; }
November 16, 2017 at 2:24 am #877307I’ve searched and searched on this. From what I’ve read, using the $wptitle hook like that should affect the <title></title> part as well….but it isn’t.
November 16, 2017 at 2:49 am #877309Ok….after hours of trial and error of different codes…I finally figured it out.
Let me preface this by explaining my reasons. I have some pre-designed business cards I sell. When using the product grid, if I have the title include the words “Business Cards” at the end, then it is really easy for the title to get long and then bleed over into a new line. Visually to my OCD it looked odd. Besides, they are listed on a page for Business Cards already…people know what they are. So I wanted to figure out a way to dynamically strip the words “Business Cards” from the titles in the product grid. Using something like a str_replace doesn’t work if you’re using a theme widget like that. So my only option was to remove the words from the product title itself. That meant that I had to come up with a way to dynamically add the words back into the product page’s <title> and SEO meta tags so it gets picked up for a more relevant search result.
So here’s how I did it so it ONLY applies to a product in a specific category. Can be easily modified for all kinds of uses… Logically you’d think the theme hook should do this automatically since it changes it everywhere else. Maybe it was overlooked or is a bug. I can’t see why anyone would want to only change the SEO meta tag entries and not the <title> tag along with it to match.
// change the <title> tag add_filter('document_title_parts', 'dq_override_post_title', 10); function dq_override_post_title($title){ // change title for predesigned bcards only if(is_product() && has_term( 'Business Cards', 'product_cat' )){ // change title parts here $title['title'] = get_the_title().' Business Cards'; $title['site'] = 'Spiderfly Studios'; //optional } return $title; }
November 17, 2017 at 1:41 pm #878050Hi,
Great, glad you found a solution for your problem and thanks a lot for sharing. Much appreciated!
Best regards,
RikardNovember 18, 2017 at 12:07 am #878331Well…it’s more of a patch as the filter provided by your team shown above should do this. I would call this a bug as it changes the title everywhere but the <title> element in the header.
November 21, 2017 at 3:59 pm #879788Hi,
Thank’s for reporting this.
The function avia_set_title_tag() had been deprecated with Enfold 3.6, because WP introduced _wp_render_title_tag() with 4.1. Since that time our theme is using the default WP way to output the title.
So the hook you are using is the only and correct way to update the title.
Best regards,
Günter -
AuthorPosts
- The topic ‘Title tags not completely changing’ is closed to new replies.