- This topic has 5 replies, 2 voices, and was last updated 3 years, 4 months ago by
David.
-
AuthorPosts
-
November 29, 2022 at 9:29 am #2441482
Frank
Hi,
I created a block in elements that contains a single button.
How can I hook this block in all my posts before a particular H2 headline?
For example, if I want to hook the button in directly before the second H2 headline of every post.Thank You,
FrankNovember 29, 2022 at 10:04 am #2441541David
StaffCustomer SupportHi there,
there are no hooks inside you content, but you can filter the_content, to insert a shortcode which in turn outputs a Hook for you. Add this PHP Snippet to do that:
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(); }); add_filter( 'the_content', 'insert_element_eleventh', 30 ); function insert_element_eleventh( $content ) { global $post; $inserted_hook = do_shortcode('[portable_hook hook_name="after_second_heading"]'); if ( is_single() && ! is_admin() ) { return prefix_insert_after_paragraph( $inserted_hook, 2, $content ); } return $content; } function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) { $closing_p = '</h2>'; $paragraphs = explode( $closing_p, $content ); foreach ($paragraphs as $index => $paragraph) { if ( trim( $paragraph ) ) { $paragraphs[$index] .= $closing_p; } if ( $paragraph_id == $index + 1 ) { $paragraphs[$index] .= $insertion; } } return implode( '', $paragraphs ); }It will create a custom hook called:
after_second_headingYou can change the hook name if you want on this line:
$inserted_hook = do_shortcode('[portable_hook hook_name="after_second_heading"]');And this line the number
2is where it sets which H2 to appear after:return prefix_insert_after_paragraph( $inserted_hook, 2, $content );November 29, 2022 at 10:19 am #2441569Frank
Hi, David
Where should I add this php snippet?
If I add it to the “Additional CSS” in Generate Press, will the after_second_heading hook appear in the list?
Thanks,
FrankNovember 29, 2022 at 10:33 am #2441596David
StaffCustomer SupportThe code is PHP – this article explains how to add it to your site:
https://docs.generatepress.com/article/adding-php/
Then in your Block element, set the Hook to
Customand in the custom hook field paste in:after_second_headingNovember 29, 2022 at 11:04 am #2441650Frank
Hi, David
That worked great!
Can the code be changed to insert the block element BEFORE the second heading (before second H2)?Thanks,
FrankNovember 30, 2022 at 3:46 am #2442494David
StaffCustomer SupportIn the code:
Remove this:
function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) { $closing_p = '</h2>'; $paragraphs = explode( $closing_p, $content ); foreach ($paragraphs as $index => $paragraph) { if ( trim( $paragraph ) ) { $paragraphs[$index] .= $closing_p; } if ( $paragraph_id == $index + 1 ) { $paragraphs[$index] .= $insertion; } } return implode( '', $paragraphs ); }And add this:
function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) { $closing_p = '<h2>'; $paragraphs = explode( $closing_p, $content ); if ($paragraphs) { $paragraphs[0] = $paragraphs[0].'<h2>'.$insertion; } return implode( '<h2>', $paragraphs ); } -
AuthorPosts
- You must be logged in to reply to this topic.