Skip Navigation

[Resolved] How to call an archive page ID in Code Snippets to apply a hook (re-opened)

This thread is resolved. Here is a description of the problem and solution.

Problem:
The customer wanted to target specific archive pages, to include specific GeneratePress hook elements.

Solution:
Shared some code adjustments, accordingly.

Relevant Documentation:
n/a

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 4 replies, has 2 voices.

Last updated by graemeH-3 1 year, 11 months ago.

Assisted by: Waqar.

Author
Posts
#2372267

This is a continuation of a auto-closed thread,

https://toolset.com/forums/topic/how-to-call-an-archive-page-id-in-code-snippets-to-apply-a-hook/

Minesh was most recently helping me.

The latest code Minesh sent looks very promising but I have a question as to how does this make sure to get applied only to one taxonomy at a time?

I have multiple taxonomies which have state and city levels and need different element IDs for the state and city levels in the different taxonomies.

Was this most recent code sent supposed to be added to the code I have using wpv_filter_force_wordpress_archive to set the city and state archive templates?

This code looks good but I'm confused if I'm supposed to be including this within a larger function to make sure that only one taxonomy is looked at at a time?

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 );

Thanks for the help!

#2372539

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

Thank you for contacting us and I'd be happy to assist.

I've read through the related support ticket and can understand what you're aiming for.

As for your specific question, about how to make Minesh's code apply only to a specific taxonomy, you can include a conditional check to evaluate "$term->taxonomy".

For example:


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 );

You'll replace "target_tax_1_slug" with the actual slug of the taxonomy that you want this code to apply to. And you can include more conditional blocks like this within this same function for the other taxonomies too.

And this code will work independently and it shouldn't be added inside the custom code that I shared earlier for the dynamic archive assignment.
( ref: https://toolset.com/forums/topic/how-to-properly-create-a-page-for-a-top-level-taxonomy/#post-2324891 )

Different plugins execute their hooks differently and at different execution points. I won't be able to test and confirm/deny whether you can get the Toolset archive's "$template_id" in the function for the "generate_hook_element_display" filter, because it involves GeneratePress Premium and is beyond the scope of support that we can provide over the support forum. But, based on what GeneratePress support has shared, Minesh's code should work, theoretically.

We can just help you with the pointers and for more personalized assistance around the WordPress and PHP code customizations, we'll encourage hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

On a personal note, I feel using the GeneratePress Hooks is a very complicated way to include the JSON-LD code. It is not an absolute requirement that the JSON-LD code has to be included at the head of the page. I'll recommend also testing it by adding it directly inside the respective WordPress Archive in an HTML block within the script tags (<script>...</script>).

regards,
Waqar

#2373259

Thank you Waqar, this worked perfectly!

Even adding the other taxonomies in, and assigning the correct element hooks, worked great.

Reading your comment regarding just adding the JSON+LD code via simple HTML on the archive page.... Lol, in my inexperience, I didn't realize I could do this... Or maybe it was earlier on and I had an error in the html and didn't think it could work.

You're right, it probably would have been cleaner and easier to add the code this way... However, now that it works adding it to the header, I don't see any reason now why I wouldn't want to use your piece of code there to add it to the header instead of the body.

So long as you don't see any reason why NOT to use the code you gave me there, I like this solution.

Would there be any reason why you think this wouldn't work or could cause any issues?

If not, it seems to work well from my perspective, and gets that JSON in the header instead of the body as well 🙂

Thanks again!

#2373907

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thanks for the update and glad that the code worked.

The note that I added was only out of concern about managing the JSON-LD code through multiple elements and hooks. But, if you find this management straightforward and the custom code is working, as expected, then it is perfectly safe to carry on with this approach.

#2374247

Great!

Thanks again for your valuable input Waqar!

🙂

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.