- This topic has 36 replies, 3 voices, and was last updated 3 years, 3 months ago by David.
-
AuthorPosts
-
October 20, 2020 at 12:07 am #1496512Eric
Hey guys I just had another follow-up question regarding this code you guys gave me above. I have a lot of posts and the sitewide code you guys gave me has been working like a charm! My only roadblock is I don’t know how to assign it to a specific tag or use a different variation of the PHP for different groups of pages.
I was wondering how can I assign a set of functions but only to work for a specific post tag or category? Here is the code I’ve used below:
GROUP 1
add_filter( 'generate_get_the_title_parameters', function( $params ) { if ( is_singular() ) { $params = array( 'before' => sprintf( '<h1 class="entry-title"%s>', 'microdata' === generate_get_schema_type() ? ' itemprop="headline"' : '' ), 'after' => '</h1>', ); } return $params; } ); function db_change_content_h2( $content ) { $content = str_ireplace( '<h2>', '<p class="styleA">', $content ); $content = str_ireplace( '</h2>', '</p>', $content ); return $content; } add_filter( 'the_content', 'db_change_content_h2' ); function db_change_content_h3( $content ) { $content = str_ireplace( '<h3>', '<p class="styleB">', $content ); $content = str_ireplace( '</h3>', '</p>', $content ); return $content; } add_filter( 'the_content', 'db_change_content_h3' );
…and here is GROUP 2:
add_filter( 'generate_get_the_title_parameters', function( $params ) { if ( is_singular() ) { $params = array( 'before' => sprintf( '<h2 class="entry-title"%s>', 'microdata' === generate_get_schema_type() ? ' itemprop="headline"' : '' ), 'after' => '</h1>', ); } return $params; } ); function db_change_content_h2( $content ) { $content = str_ireplace( '<h2>', '<p class="styleC">', $content ); $content = str_ireplace( '</h2>', '</p>', $content ); return $content; } add_filter( 'the_content', 'db_change_content_h2' ); function db_change_content_h3( $content ) { $content = str_ireplace( '<h3>', '<p class="styleD">', $content ); $content = str_ireplace( '</h3>', '</p>', $content ); return $content; } add_filter( 'the_content', 'db_change_content_h3' );
How can I assign GROUP 1 to only apply to posts tagged with “Recipes” and GROUP 2 to posts tagged with “Restaurants”? Or perhaps EXCLUDING tags is better? What do you guys think is best to do? Also, is there any specific behavior to watch out for if a post is tagged/assigned to use BOTH groups? Will that crash my site?
Thanks again for all the help with this, and I hope to hear from you again soon!
October 20, 2020 at 3:09 am #1496686DavidStaffCustomer SupportYou can use one of the many Conditional Tags in WordPress:
https://codex.wordpress.org/Conditional_Tags#has_term
The
has_term()
tag allows you to check if the a post includes a term – heres an example:https://developer.wordpress.org/reference/functions/has_term/#comment-1892
So your conditionals would look something like this:
if ( is_singular() && has_term('', 'recipes') ) {
October 20, 2020 at 8:32 pm #1497828EricFantastic… thank you for your help, David! OK I’ll dive into this and try to get it going 🙂
October 21, 2020 at 1:45 am #1498014DavidStaffCustomer SupportYou’re welcome
November 6, 2020 at 11:27 pm #1521328EricHi again GPress Team! OK so I’ve come across an issue with one site, wherein for some reason all my post titles are using the “Single Content Title” rather than “Headings 1 (H1)”. I don’t really know why this happened, but the following “entry-title” code will not work in this instance:
add_filter( 'generate_get_the_title_parameters', function( $params ) { if ( is_singular() ) { $params = array( 'before' => sprintf( '<h2 class="entry-title"%s>', 'microdata' === generate_get_schema_type() ? ' itemprop="headline"' : '' ), 'after' => '</h1>', ); } return $params; } );
It doesn’t seem to affect the “Single Content Title” h1s vs the “Headings 1 (H1)” h1s and will not swap out. Is there something I can change in the code above to make it work for single content titles instead?
Here is an image of the 2 different h1 settings in the customizer… https://pasteboard.co/JzdTAMS.png
Is there a way to somehow switch which heading is used on the site? I don’t know where exactly I may have set it if that’s the case. Maybe I switched them by accident!
November 7, 2020 at 7:40 am #1521798DavidStaffCustomer SupportIn your code you have:
'after' => '</h1>'
Which is probably confusing things as your before tag is a H2.
But if you’re changing the title to a H2 then this should not inherit either of the H1 styles.
November 7, 2020 at 10:50 am #1521947EricHmmm, OK this is actually the exact code you gave me earlier in the thread, but yes I noticed that “after </h1>” error and changed it to h2 as well.
My issue though is not that it is still being controlled by the customizer, but more that I can’t seem to get the code to swap the entry title from h1 to h2, like it did for all my other sites. This one site has all the entry titles in the “Single Content Title” field and for some reason the code you gave me above has no effect on it.
So, I am trying to figure out a way to either:
1. Modify the code you gave me to swap out “Single Content Title” entry titles INSTEAD of the usual “Heading 1 (H1)”
OR…
2. Somehow change all my current post titles to become “Heading 1 (H1)”-type entry titles, so that the code will work as expected.
It seems that for some reason this particular site defaults/delegates all the post titles to the “Single Content Title” and not the regular “Heading 1 (H1)” like all the others, and the code will just not swap them out in this situation.
November 7, 2020 at 11:59 am #1521983TomLead DeveloperLead DeveloperHi there,
I’m not entirely sure what you mean. Are you trying to get your H1 titles to use the H1 options from the Customizer instead of the Single Content Title options in the Customizer?
If so, you should just be able to empty/clear those Single Content Title options and things will fallback to your regular H1 styles.
November 7, 2020 at 7:04 pm #1522124EricHi Tom, no sorry several weeks ago I got code from you guys that allows me to swap out my post titles from H1 to H2. This is the code below:
add_filter( 'generate_get_the_title_parameters', function( $params ) { if ( is_singular() ) { $params = array( 'before' => sprintf( '<h2 class="entry-title"%s>', 'microdata' === generate_get_schema_type() ? ' itemprop="headline"' : '' ), 'after' => '</h2>', ); } return $params; } );
This code works really well on almost all the GPress installations I’ve used it on so far and gets the job done.
Anyways, I chimed in on this thread again because the code did not work as expected on a site that I just started working on. For some reason the post titles settings on this specific site are not set with the “Heading 1 (H1)” section and are instead using the “Single Content Title” settings. I’m only mentioning it because it’s the only difference I can find between all the sites that the code worked on before, and this site where the code doesn’t swap out H1 with H2 as expected. I’m assuming that the post titles on this site are all set with “Single Content Title” settings, and are maybe named differently or something? Maybe that’s why the code won’t work?
So, yeah I just want to know how to get the code working on this site as well, which is not using the “Heading 1 (H1)” settings for some reason.
November 8, 2020 at 6:49 am #1522534DavidStaffCustomer SupportBTW I corrected the error in the original code – thanks for pointing that out.
Where can we see a post that is having the issue, it may help us understand the problem.
November 8, 2020 at 7:09 am #1522634EricOK no problem, I’ve added one of the sample URLs to “Private Information”.
November 8, 2020 at 11:37 am #1522926TomLead DeveloperLead DeveloperThat’s strange – that code should be changing that H1 to an H2. Just to confirm, what version of GP is this site using?
November 8, 2020 at 6:46 pm #1523162EricYeah, the code has worked flawlessly on all my other GP installations, but this one is not swapping out. I’ve already updated it to the latest version of GP. All the sites have the pretty much the exact same setup with a GP child theme running.
November 9, 2020 at 1:34 am #1523400DavidStaffCustomer SupportI tested the code on a clean install – latest versions of GP etc. and it worked correctly.
What other functions do you have in place ? Can you check for conflicts by temporarily removing them ?November 9, 2020 at 5:04 am #1523665EricAha! Fantastic, David… you were right about that. There was a custom function for “content-single.php” file in the child that had the exact same code, but designated for h1! I changed it and now it’s working as expected. Thank you guys! I would never have thought to start combing through the other functions myself.
-
AuthorPosts
- You must be logged in to reply to this topic.