- This topic has 12 replies, 6 voices, and was last updated 1 year, 11 months ago by Fernando.
-
AuthorPosts
-
September 20, 2021 at 9:18 pm #1937342Cynthia
Hello. I wanted my affiliate notice to show up in every post so I added a hook element that currently is set to generate after entry title. I want the notice of course to be before any of my affiliates are used but I don’t love that it is the first thing people read. Is there a way to set it so that it will just come after my first paragraph in each post? I looked through the choices but I’m not very savvy so I’m probably just missing it. Please help π Thanks
CynthiaSeptember 20, 2021 at 9:49 pm #1937353ElvinStaffCustomer SupportHi Cynthia,
We can try making a hook after the first paragraph if you want.
Add this PHP snippet:
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_featured_image', 20 ); function insert_featured_image( $content ) { global $post; $inserted_hook = do_shortcode('[portable_hook hook_name="after_first_paragraph"]'); if ( is_single() && ! is_admin() ) { return prefix_insert_after_paragraph( $inserted_hook, 1, $content ); } return $content; } function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) { $closing_p = '</p>'; $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 ); }
After activating this PHP snippet on your site, you can hook things on the first paragraph of the content using
after_first_paragraph
hook. πSeptember 22, 2021 at 3:51 pm #1939694CynthiaHey Elvin,
Thanks for the help. I’m not very tech savvy. Would you suggest I use the plugin code snippets to add the PHP code you suggested or should I set up a child theme? Those were the two suggestions I saw when I was searching how to add PHP code in generate press. Thanks in advance π
CynthiaSeptember 22, 2021 at 8:12 pm #1939785ElvinStaffCustomer SupportAny of the two choices should be fine but for your case, I’d suggest Code Snippets plugin as it’s easier to setup. π
October 25, 2021 at 6:57 pm #1975299CynthiaHey there. I finally got around to trying this and I’m screwing up somewhere because it’s not working for me. So I went with your advice and added the snippet plug-in. I then did “add new” as a css and added the code you gave me above (I’m assuming that is what I should have added it as). Then I am given the choice of run snippet everywhere (which I chose), only run in admin area, only run on site front end, only run once. Then I went in under “elements” and tried to hook to the first paragraph. However I’m not seeing after_first_paragraph option. Could you help me where I am going wrong. Thank you!
October 25, 2021 at 7:50 pm #1975320ElvinStaffCustomer SupportI then did βadd newβ as a css and added the code you gave me above (Iβm assuming that is what I should have added it as).
The code provided is a PHP snippet. It should be added as PHP. π
However Iβm not seeing after_first_paragraph option. Could you help me where I am going wrong. Thank you!
It won’t show up on the display rule because it’s a custom hook we’ve manually added.
On the “Hook” dropdown of the element, you should select “Custom”. A field will appear. That’s were you type in
after_first_paragraph
πOctober 26, 2021 at 5:18 pm #1976965CynthiaYay it worked! Thanks Elvin so much for your patience and kindness!
October 26, 2021 at 5:18 pm #1976966Cynthiaπ
October 26, 2021 at 9:05 pm #1977142ElvinStaffCustomer SupportNice one! Glad you got it to work. Thanks for letting us know. No problem. π
March 19, 2022 at 2:12 pm #2160434MohammedHi there,
thanks for that.
I’m having an issue when adding this.
You can see the element The Why trust us box after the first paragraph. There’s a table of content that is being nested with the block element. The table of content should be shown after that box.
March 20, 2022 at 5:56 am #2160835DavidStaffCustomer SupportI will reply to your topic
February 1, 2023 at 2:56 pm #2517656AndrewSorry, I mean to post this reply on another thread. Is there a way to delete this reply?
βββββββ-I just tried that and got the error “Your PHP code changes were rolled back due to an error on line 122 of file wp-content/themes/prudentreviews/functions.php. Please fix and try saving again.
syntax error, unexpected end of file”
This is the code I’m using, including the first part which is working fine.
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_featured_image’, 20 );
function insert_featured_image( $content ) {
global $post;
$inserted_hook = do_shortcode(‘[portable_hook hook_name=”after_first_paragraph”]’);
if ( is_single() && ! is_admin() ) {
return prefix_insert_after_paragraph( $inserted_hook, 10, $content );
}
return $content;
}
function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
$closing_p = ‘</p>’;
$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 );
}
add_filter( ‘the_content’, ‘insert_featured_image2’, 20 );
function insert_featured_image2( $content ) {
global $post;
$inserted_hook = do_shortcode(‘[portable_hook hook_name=”after_fiftieth_paragraph”]’);
if ( is_single() && ! is_admin() ) {
return prefix_insert_after_paragraph( $inserted_hook, 50, $content );
}February 1, 2023 at 5:30 pm #2517761Fernando Customer SupportHi Andrew,
We’ll reply in the topic you created.
-
AuthorPosts
- You must be logged in to reply to this topic.