Is this possible with Hugo?

Is it possible to show something (like a shortcode) automatically just before the first h2 heading in a single post without having to include the shortcode itself in every post? (I have over 600 posts in the concerned blog).

You can have a setting in Front Matters that triggers a specific action/code. I do that to call JQuery only on specific pages.

I already got that part figured out. But I have a specific use case for the initial question.

You can use: findRE or replace. For example:

{{ replace .Content "<h2" "<div>test</div><h2" | safeHTML }}

Note: I am not sure if the safeHTML is needed.

How would that work? For example, to place a shortcode {{< test >}} just above the first H2? I am dreading doing this manually, but I will have to if there is no workaround.

You could do it via a render hook:

But you would need to keep track of the count yourself (via .Page.Scratch)

I don’t have the knowledge to create such a hook. But I appreciate the pointers. For now, I have decided to place the shortcode before content, even though it is not desirable since it breaks the website’s desired look.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.