-
Element Gardening –> Custom Hook affiliate_hook_inside_shortcode –> Posts / All posts –> PHP Function:
The code does not contain any
affiliate_hook_inside_shortcode
, the hook name should’ve beencustom_shortcode_hook
, and the shortcode is[hooky_affiliate_shortcode]
.Element Recipe –> Custom Hook after_h2 –> Posts / all posts –> PHP Function:
Are you using
after_h2
hook? I thought you said it works here in your reply:https://generate.support/topic/template-for-affiliate-links/page/2/#post-133693 -
Topfgartenwelt
For gardening I have used an element connected to that code from David: https://generate.support/topic/template-for-affiliate-links/page/2/#post-133065
He writes: Then in your element add the custom hook of: affiliate_hook_inside_shortcode
And in your posts add the shortcode: [hooky_affiliate_shortcode]I have used now the hook name provided by you custom_shortcode_hook and it works now.
For the recipe I used the after_h2 hook and yes it worked one time. But I can’t reproduce it. It doesn’t show up. Here are my settings: https://imgur.com/a/Ybd8fPF
Greetings Kathrin
-
I have used now the hook name provided by you custom_shortcode_hook and it works now.
The first code in your reply here:https://generate.support/topic/template-for-affiliate-links/page/2/#post-134031 is the same thing as David’s code you linked. You only need to keep one of the codes and use the shortcode set in the code.
I have tried to set category post / gardening // category post / recipes but both doesn’t work. What should I do to get work?
it should work, unless there’s another function that conflicts with this function. What is the content in the block element
after_h2
?Can you try disabling all other custom functions to test?
-
Topfgartenwelt
The element / hook for gardening works fine.
For the recipes:
I have disabled all custom functions and tried to show the Affiliate Block for the recipes. It doesn’t work. If I set the hook to show before comments the block appears as it should. But set to custom hook after_h2 nothing is shown. There is also no difference if i set the element to all posts or just a specific one.
After slightly changing the PHP-Code to this:
add_shortcode('portable_hook', function($atts){ ob_start(); $atts = shortcode_atts( array( 'hook_name' => 'no foo' ), $atts, 'portable_hook' ); do_action($atts['hook_name']); return ob_get_clean(); }); function insert_hook_after_second_h2( $content ) { global $post; $inserted_hook = do_shortcode('[portable_hook hook_name="after_h2"]'); if ( is_single() && ! is_admin() ) { // Regex, um die zweite H2-Überschrift und danach zwei <p> Tags zu finden $pattern = '/(<h2[^>]*>.*?<\/h2>.*?<p[^>]*>.*?<\/p>.*?<p[^>]*>.*?<\/p>)/s'; $count = 0; // Variable zum Zählen der H2 $content = preg_replace_callback($pattern, function($match) use ($inserted_hook, &$count) { $count++; // Füge den Hook nur nach der zweiten H2 und zwei <p> Tags ein if ($count == 2) { return $match[0] . $inserted_hook; } return $match[0]; // Ändere nichts nach der ersten H2 }, $content); } return $content; } add_filter('the_content', 'insert_hook_after_second_h2');
it works for gardening and recipes. I have set it to post category gardening / recipes.
Greetings Kathrin
-
Is this the content you inserted via the
after_h2
hook?
https://app.screencast.com/i2pFwiZfTiju6
- You must be logged in to reply to this topic.