[Resolved] PHP filtering questions (title tag and headings)

Home Forums Support PHP filtering questions (title tag and headings)

  • This topic has 36 replies, 3 voices, and was last updated 4 months ago by David.
Viewing 15 posts - 16 through 30 (of 37 total)
  • Author
    Posts
  • #1496512
    Eric

    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!

    #1496686
    David
    Staff
    Customer Support

    You 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') ) {

    #1497828
    Eric

    Fantastic… thank you for your help, David! OK I’ll dive into this and try to get it going 🙂

    #1498014
    David
    Staff
    Customer Support
    #1521328
    Eric

    Hi 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!

    #1521798
    David
    Staff
    Customer Support

    In 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.

    #1521947
    Eric

    Hmmm, 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.

    #1521983
    Tom
    Lead Developer
    Lead Developer

    Hi 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.

    #1522124
    Eric

    Hi 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.

    #1522534
    David
    Staff
    Customer Support

    BTW 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.

    #1522634
    Eric

    OK no problem, I’ve added one of the sample URLs to “Private Information”.

    #1522926
    Tom
    Lead Developer
    Lead Developer

    That’s strange – that code should be changing that H1 to an H2. Just to confirm, what version of GP is this site using?

    #1523162
    Eric

    Yeah, 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.

    #1523400
    David
    Staff
    Customer Support

    I 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 ?

    #1523665
    Eric

    Aha! 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.

Viewing 15 posts - 16 through 30 (of 37 total)
  • You must be logged in to reply to this topic.