[Resolved] Adding a header hook to all pages using a certain Toolset Archive Template

Home Forums Support [Resolved] Adding a header hook to all pages using a certain Toolset Archive Template

Home Forums Support Adding a header hook to all pages using a certain Toolset Archive Template

Viewing 14 posts - 1 through 14 (of 14 total)
  • Author
    Posts
  • #2207072
    Graeme

    Hi there,

    I am using the Toolset plugin to create a business listings directory.

    I have archive pages for a certain taxonomy which includes sub taxonomies of state, and then city below that.

    The state and city pages use different archive page templates so that I can create custom breadcrumbs and search options for both levels of the taxonomy.

    PROBLEM: when I go to add a hook with generatepress elements, it lets me select only the entire top level taxonomy, which makes the breadcrumbs apply to both state and city levels which doesn’t work because they each need a different structure.

    I found another support topic post which references this help page,
    https://docs.generatepress.com/article/generate_hook_element_display/

    This seems promising, but I’m unsure how to use this to apply only to a certain toolset archive template.

    Could you please show me an example?

    The code,

    add_filter( ‘generate_hook_element_display’, function( $display, $element_id ) {
    if ( 10 === $element_id && is_author( ‘Tom’ ) ) {
    $display = true;
    }

    return $display;
    }, 10, 2 );

    Does my hook code come here, underneath the filter? Or should it go within the filter code above.

    I’m not sure if it makes a difference, but the code which I am trying to hook into the header is JSON-LD code, which using a short code can then populate my breadcrumb data correctly for these pages.

    Should the if statement be changed to something like this? if ( 10 === $element_id && $template_id = ‘7590’ )

    Thank you for any and all direction on this!

    G

    #2207108
    Fernando
    Customer Support

    Hi Graeme,

    You should be able to set a Hook Element to display on a specific Category/sub-category Archive page through its display Rules.

    To have an idea of what you’re trying to do, may we get an example of states and cities and how they’re grouped/categorized?

    May we also know how you’re trying to set up the Display rules currently?

    Example: https://share.getcloudapp.com/L1uRZ1WK

    Also see: https://share.getcloudapp.com/d5u9dKNR

    Kindly let us know. Hope to hear from you soon. 🙂

    #2207260
    Graeme

    Thanks for the response Fernando.

    To clarify, yes, I’ve looked through the display rules thoroughly.

    To clarify the category and sub category pages I’ll use an example.

    Taxonomy Archive : Laundromats
    Sub Taxonomy level 1 archive pages : All states, ex. Florida, California, Washington, Oregon, Colorado etc.
    Sub Taxonomy level 2 archive pages : all cities from each state, like for Florida : Tampa, Miami, etc.
    URL structure example: .com/laundromats/florida/ this is an archive page of all laundromats in Florida, .com/laundromats/florida/miami this is an archive page of all laundromats in Miami, .com/laundromats/florida/tampa this is an archive page of all laundromats in Tampa. And so on for all states and their cities.

    I have 1 archive page template for the state level, and a different one for the city level and they both need their own structured data and therefore their own hook.

    The display rules only allow me to select an entire archive, ex. Laundromat Archive, this is great, EXCEPT, then I can either choose ALL, which is both state and city pages, which doesn’t work… Or I can select them manually, one by one, which also doesn’t really work because I have close to 500 archive pages between the states and city pages…

    Which brings me to thinking the only solution I’ve got here would be to work the filter code somehow, or maybe nest some if statement somehow into my JSON+LD code which I’m putting in the hook?

    Any pointers would be appreciated.

    Thanks
    G

    #2207309
    Fernando
    Customer Support

    To clarify, do you have one archive template for the city level as well?

    If so, if you have much less states, you may choose all, and then exclude the states.

    And then, in you other Element, only display it to the states.

    The same logic apply if you use generate_hook_element_display, wherein you could probably check if a category has a parent category with a specific ID:

    add_filter( 'generate_hook_element_display', function( $display, $element_id ) {
    if(!is_admin()){
    $cat = get_queried_object();
        if ( 10 === $element_id && in_array( $cat->parent, [1,2,3,4,5] ) ) {
            $display = true;
        }
    }
        return $display;
    }, 10, 2 );

    or use is_post_type_archive( 'my_custom_post_type' ) for the parents like

    add_filter( 'generate_hook_element_display', function( $display, $element_id ) {
    if(!is_admin()){
    $cat = get_queried_object();
        if ( 10 === $element_id && is_post_type_archive( 'my_custom_post_type' ) && !in_array( $cat->parent, [1,2,3,4,5] ) ) {
            $display = true;
        }
    }
    
        return $display;
    }, 10, 2 );

    Kindly replace 10 with the ID of the element which could be found in the URL when editing the Element. The same goes for a category, the ID can be found in the URL: https://share.getcloudapp.com/lluEpB1B and you may use this to replace 1,2,3,4,5.

    It may be easier to do this through the Display rules depending on your structure size.

    Hope this helps!

    #2208390
    Graeme

    Not only are there 50 states… But I’m planning to use this application for another 5 or 6 directories, each with their own 50 states.

    It will get way too messy to try and deal with this by manually selecting each.

    add_filter( ‘generate_hook_element_display’, function( $display, $element_id ) {
    $cat = get_queried_object();
    if ( 10 === $element_id && in_array( $cat->parent, [1,2,3,4,5] ) ) {
    $display = true;
    }

    Does my JSON-LD code go here?

    return $display;
    }, 10, 2 );

    Or does my JSON-LD code go here?

    I assume when you refer to specific ID’s of parent categories you’re referring to the individual states? So I would need to go find out what each individual state’s ID is, all 50 of them for my numerous directories, (I guess I find these in individual category pages), and then manually enter them all into this list? Is there a way to download a list of all the parent categories (states) within a taxonomy so I can just copy paste all these ID’s for each directory/top level taxonomy once I create it?

    The ID, (10), of the element, what I see in the URL shows a “post=1234”, this is the element ID you’re referring to? This needs to go both in the if statement and at the end of the code? Two replacements of “10” with the “post=1234” number?

    Using the, “is_post_type_archive( ‘my_custom_post_type’ )”, as you mentioned, still involves manually scraping all of the state category ID’s together?

    Is there no way to apply this hook to a TEMPLATE design? As I said, I have TWO templates, one for city, and one for state levels.

    If I can simply apply the hook based on a template ID number, then I can just input 1 template number in my hook filter, instead of 50…

    The archive templates I created were using Toolset, they have a template ID number for each template I’ve created. I’m just not sure how to reference or call these templates, like $toolset_template_ID or something, I don’t know.

    Note: if this makes a difference, I got help from Toolset support to work out some PHP code which checks whether an archive page has a parent or not, before applying the template; this is how I’m applying a separate template to the state & city level archive pages.

    I think this should be a pretty simple bit of code to achieve what I’m trying to do… Once I know where to put the JSON+LD code in the element, and how to filter to a specific archive template…

    Thanks again for the help!
    G

    #2208431
    Fernando
    Customer Support

    You’re right, it seems that there is a way to get the template_id from the plugin you’re using. Using this may be a much easier way to create a logical statement. Maybe you could reach out to their support for assistance: (I saw this for instance: https://toolset.com/forums/topic/generateblocks-css-not-working-on-toolset-wordpress-archives-template/)

    So basically, you’re code might be (just a sample):

    add_filter( 'generate_hook_element_display', function( $display, $element_id ) {
    if(!is_admin()){
        if ( 123 === $element_id && 456 === $template_id ) {
            $display = true;
        }
    }
        return $display;
    }, 10, 2 );

    where you need to find a way to get $template_id. It would be best to reach out to the support of this plugin regarding this.

    Ask them how to get the template id, and use that in your conditional statement instead of using IDs of the parents which would be more tedious.

    To reiterate, you may create a Hook Element with the display rules left as blank/defualt, and add your JSON+LD code in the Hook Element: https://share.getcloudapp.com/wbuz4XqA

    Then, add the snippet above through a plugin like Code Snippets, and modify the logic to your preference – replace (456 === $template_id) with your logic. You can do away with the other conditional statements previously suggested and just use this.

    Lastly, you would only need to replace the first 10 in the previous code, which I have now replaced with 123. The second 10 is for the priority. Replace 123 with the ID of the Hook Element.

    Hope this clarifies. 🙂

    #2208476
    Graeme

    Hi Fernando,

    thanks for this.

    I think this is starting to make a little more sense.

    The filter code add_filter etc. does NOT go in the element coding at ALL.

    The filter code goes into the general PHP of the website, which I have previously added to using Code Snippets.

    You may be seeing now my lack of understanding and context of how this works as to why I’m asking these questions the way I am haha.

    This makes sense, JSON-LD in element code, the post ID from the element URL use in the filter added through Code snippets.

    For setting the filter for multiple archive pages, could I just repeat the code as we have it here, with the new template and element numbers, all in one Code Snippet? Or should I be creating a new Code Snippet for each filter?

    Thank you for the link to that article in Toolset support, are there any elements of it I should be looking more closely at? I tried a bunch of variations, to try and figure out the $id_variable to check in the if statement but not lucky so far.

    Again forgive the lack of context with this, the $template_id as we’ve called it, so I know how to refer to this correctly, is like part of PHP language or WordPress backend?

    Can you help me on the language and how to actually word the question I need to ask of Toolset support on this so I can be specific?

    I’m not really sure how to accurately name what I am asking for haha.

    Thank you again ! Learning a lot over here 🙂
    G

    #2208487
    Fernando
    Customer Support

    Yes, that’s correct, you can place your JSON-LD code in a Hook Element, and if you have two codes, one for states and the other for cities, you’ll need to create two Hook Elements as well.

    With that said, if you have two Hook Elements, in code snippets, you can just have one add_filter with an if and else if statement, specifically, something like:

    add_filter( 'generate_hook_element_display', function( $display, $element_id ) {
    if( !is_admin() ) {
        if ( 123 === $element_id && 456 === $template_id ) {
            $display = true;
        } else if ( 321 === $element_id && 654 === $template_id ) {
            $display = true;
        }
    }
        return $display;
    }, 10, 2 );

    You’ll need to replace 123 with the id of the first Hook Element and 321 with the other.

    So, you basically need to ask how them to get the template_id in an archive page you’ve created so we can use it in a sense like 456 === $template_id and 654 === $template_id.

    This is code which can added through Code Snippets is PHP.

    Reference for hook elements: https://docs.generatepress.com/article/hooks-element-overview/

    Hope this clarifies. 🙂

    #2216905
    Graeme

    So far Toolset support hasn’t been able to tell me how to call the template_id that I need, I’m waiting for the next response from them, still hopeful.

    In the meantime I was looking back at one of the code suggestions you had above,

    add_filter( ‘generate_hook_element_display’, function( $display, $element_id ) {
    if(!is_admin()){
    $cat = get_queried_object();
    if ( 10 === $element_id && in_array( $cat->parent, [1,2,3,4,5] ) ) {
    $display = true;
    }
    }
    return $display;
    }, 10, 2 );

    Is there a way to add in to the code above to apply only to one particular taxonomy?

    And then instead of checking parent ID, just check if there is a parent at all?

    If we could apply this code to ONE taxonomy, and then simply check, does archive page have a parent = city, and no parent = state page and apply the element ID to those pages accordingly.

    Thanks again for all the help, hopefully we can find a way to do this 🙂

    G

    #2217480
    David
    Staff
    Customer Support

    Hi there,

    this is kinda tricky and i a not sure it works.

    Lets assume i have a Parent State Element 123 and a Child City Element 456 . And i have both of them set to Display on the Categories so both are visible.

    Then you could use these conditions to set the display condition to false:

    // has a parent
    add_filter( 'generate_hook_element_display', function( $display, $element_id ) {
        $term = get_queried_object();
        if ( 123 === $element_id && $term->parent != 0 ) {
            $display = false;
        }
        return $display;
    }
    
    // has no parent
    add_filter( 'generate_hook_element_display', function( $display, $element_id ) {
        $term = get_queried_object();
        if ( 456 === $element_id && $term->parent == 0 ) {
            $display = false;
        }
        return $display;
    }
    #2234598
    Graeme

    Hi David,

    thanks for this it looks promising.

    I also had the following code returned from the Toolset team, similar to yours, only one thing I’m still confused by on this is making sure this only applies to one taxonomy as I have multiple taxonomies with city and state levels and they all require different element hooks for their different breadcrumb lists.

    Toolset code,

    add_filter( 'generate_hook_element_display', function( $display, $element_id ) {
    if( !is_admin() ) {
     
           $term = get_queried_object();
            // check if the current term has some parent
            if($term->parent > 0 and 111== $element_id ) {  // city condition
               $display = true;
           } else  if($term->parent ==0 and 222== $element_id ) {  // state condition
               $display = true;
            }
     
    }
    return $display;
    }, 10, 2 );

    Maybe it needs to fit in somehow within this wpv_filter_force_wordpress_archive function I’m running in Toolset Settings for added code to assign the city and state archives to their correct templates?

    add_filter( ‘wpv_filter_force_wordpress_archive’, ‘wpv_filter_force_wordpress_archive_tax_1’, 30, 2 );
    function wpv_filter_force_wordpress_archive_tax_1( $wpa_assigned, $wpa_loop ) {

    Thank you again 🙂

    #2234946
    David
    Staff
    Customer Support

    The method i proposed uses the $display = false; so it will disable the element when the condition does not apply. So you can set the Elements Display Rules > Location to the Taxonomy you want it applied to.

    #2235732
    Graeme

    Got it, thank you.

    Makes sense.

    Toolset came back with this code as well, which I’ll post here in case anyone else is using this info.

    add_filter( 'generate_hook_element_display', function( $display, $element_id ) {
        if( !is_admin() ) {
            $term = get_queried_object();
            // check if specific taxonomy
            if ($term->taxonomy == 'target_tax_1_slug') {
                // check if the current term has some parent
                if( ($term->parent > 0) && (111 === $element_id) ) { // city condition
                    $display = true;
                } else if( ($term->parent == 0) && (222 === $element_id) ) { // state condition
                    $display = true;
                }
            }
        }
        return $display;
    }, 10, 2 );

    Replacing ‘target_tax_1_slug” with the slug of the taxonomy to apply it to.

    The code above seems to work well.

    I also realize now, sheepishly, that I can actually add the JSON+LD code that I want via simple HTML added to the archive page templates.

    Doing it this way would have saved myself the trouble and everyone’s time helping me, but then the code would have been in the body and not the header.

    Seems like this is a great solution, which works, and puts the JSON+LD code into the header instead of the body, so, all in all, great solution, and thank you so much for the help!

    #2236320
    David
    Staff
    Customer Support

    Awesome – thanks for sharing your final solution!

Viewing 14 posts - 1 through 14 (of 14 total)
  • You must be logged in to reply to this topic.