Skip Navigation

[Resolved] Hooking to a custom archive page loop

This support ticket is created 5 years, 2 months ago. There's a good chance that you are reading advice that it now obsolete.

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.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

Tagged: 

This topic contains 4 replies, has 2 voices.

Last updated by jacoboG-2 5 years, 2 months ago.

Assisted by: Nigel.

Author
Posts
#1386615

Hi,

I'm have a custom archive page view for a custom post type working (both done with Toolset). Now I face the problem that, for each displayed post one each page of the archive, I would need to add some code to the <head> section of the page (Schema metadata).

I know how to do this for normal pages, but us there anyway to hook to that custom archive loop from a PHP function in my functions.php or whatever?

Thank you very much,

Jake

#1386805

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Jake

Hooking "into the loop" won't help because it is too late to add anything to the head, by the time WordPress gets to the loop it is already building the body.

You could use a plugin to add schema data to the head, but if doing it yourself you may want to use the wp_head hook so that you can add content to the head section. Your code would want to check you are on the relevant archive page before adding the relevant content.

https://developer.wordpress.org/reference/hooks/wp_head/
https://developer.wordpress.org/reference/functions/is_post_type_archive/

Alternatively you can modify the markup of the output so that it includes schema-related attributes.

You may find the links in this earlier reply helpful: https://toolset.com/forums/topic/creating-a-layout-on-a-divi-site-i-want-to-add-events-schema/#post-1353637

If there is something specific you get stuck with, let me know.

#1386815

Hey Nigel!

Thanks a ton for the answer.

The problem with wp_head and is_post_type_archive is that I would need to add schema to the head for each and every element of the query. AFAIK I can't do that there as I need to retrieve the data for each displayed post and put that in the head with JSON-LD. Am I missing something?

Although I'd prefer using JSON-LD I think the path of less resistance is going to be adding microdata to the templates, hehe! But wondering if there's a different option (not modifying theme templates) to make using JSON-LD a possibility.

I know it's not the typical use case for Toolset, but preferred to ask before implementing everything.

Thanks a lot!

#1386825

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

You would basically have to recreate iterating over the loop using wp_head because when the loop is actually iterated over the head is history already, it's too late to add to it.

So if you want the schema data to come from the posts which appear in the index themselves then I don't see any alternative to adding the schema attributes to the markup (the values for which you can generate using Toolset shortcodes if needed).

#1386839

My issue is resolved now. Thank you!