- This topic has 15 replies, 4 voices, and was last updated 2 years, 1 month ago by Fernando.
-
AuthorPosts
-
March 14, 2022 at 3:45 pm #2154629Jodie
Hi folks,
I’d like to have text for disclaimer with a link to the disclaimer page automatically applied below my first image on my posts. What’s the best way to do this please?
Thanks for your help!March 14, 2022 at 3:51 pm #2154633LeoStaffCustomer SupportHi Jodie,
Take a look at this video. It should be very similar to what you are looking for:
March 14, 2022 at 5:35 pm #2154688JodieHi Leo,
Thanks for that. I’ve tried doing that but the priority doesn’t change the position of the aff disclaimer. The video mentioned something about the featured image also being added by a hook? Any tips? Thanks!
Cheers Jodie
March 14, 2022 at 5:57 pm #2154699YingStaffCustomer SupportHi Jodie,
The priority doesn’t work because the image in your post is not a featured image but an image block manually added within the content.
Unfortuanly there’s NO built-in hook within the content.
Although you can try this solution to insert the disclaimer after the first paragraph:
https://generatepress.com/forums/topic/element-after-first-paragraph/March 14, 2022 at 6:07 pm #2154703JodieThanks very much Ying. You mention featured image. What do you recommend there? Is it possible to add a hook for a featured image?
March 14, 2022 at 7:33 pm #2154753Fernando Customer SupportHi Jodie,
In reference to this Hooks Visual Guide, using after_entry_hook should place somewhere under the Featured image of the post.
However, since you’re not using Featured images, as mentioned by Ying, you may create your own portable hook through a shortcode. This in turn will allow you to place Elements in any location you wish.
For instance, you may add this code:
function your_shortcode($atts, $content = null) { ob_start(); do_action('hook_name'); return ob_get_clean(); } add_shortcode('portable_hook', 'your_shortcode');
Adding PHP: https://docs.generatepress.com/article/adding-php/#code-snippets
Adding it through Code Snippets should work.
Then, after adding this PHP code, you may add the shortcode,
portable_hook
, manually to your preferred location.Example: https://share.getcloudapp.com/mXuYE7QA
Also see: https://share.getcloudapp.com/OAu6RxvG
Now you can hook onto this shortcode through a Custom hook,
hook_name
.See: https://share.getcloudapp.com/QwuLeQJo
Through this, you may now place any Element in your preferred location. Just place the shortcode, and hook to this shortcode through the hook name you’ll specify.
Kindly replace
potable_hook
andhook_name
if you wish.Hope this clarifies. 🙂
March 14, 2022 at 7:49 pm #2154767JodieThank you very much Fernando. In order for this to work, do I have to add the shortcode to every single post I want the disclaimer to appear? If so, is there a way to add it to all posts in one action?
March 14, 2022 at 8:23 pm #2154785Fernando Customer SupportWith the code provided, you’ll need to add the shortcode to each post.
If you want it automatically added after the first images of your Post, here is a code you may try:
add_shortcode('portable_hook', function($atts){ ob_start(); $atts = shortcode_atts( array( 'hook2_name' => 'no foo' ), $atts, 'portable_hook' ); do_action($atts['hook2_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 hook2_name="after_first_image"]'); if ( is_single() && ! is_admin() ) { return prefix_insert_after_first_image( $inserted_hook, 1, $content ); } return $content; } function prefix_insert_after_first_image( $insertion, $image_id, $content ) { $images = preg_split('/(<img[^>]+\>)/i', $content, -1, PREG_SPLIT_DELIM_CAPTURE); foreach ($images as $index => $image) { if ( $image_id == $index ) { $images[$index] .= $insertion; } } return implode( '', $images ); }
Adding PHP: https://docs.generatepress.com/article/adding-php/#code-snippets
As mentioned, adding it through Code Snippets should work.
Then, you may add an Element and use the hook for the code as such: https://share.getcloudapp.com/BluopBjp
Specifically, use Custom Hook –
after_first_image
.Kindly let us know how it goes. 🙂
March 14, 2022 at 9:19 pm #2154813JodieHi Fernando, I noticed there’s some code within a grey box and some out of it. We’re using a generatepress child theme.
When I tried to add the below code to the functions.php file in my child theme, it showed an error.
add_shortcode('portable_hook', function($atts){ ob_start(); $atts = shortcode_atts( array( 'hook2_name' => 'no foo' ), $atts, 'portable_hook' ); do_action($atts['hook2_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 hook2_name="after_first_image"]'); if ( is_single() && ! is_admin() ) { return prefix_insert_after_first_image( $inserted_hook, 1, $content ); } return $content; } function prefix_insert_after_first_image( $insertion, $image_id, $content ) { $images = preg_split(‘/(<img[^>]+\>)/i’, $content, -1, PREG_SPLIT_DELIM_CAPTURE); foreach ($images as $index => $image) { if ( $image_id == $index ) { $images[$index] .= $insertion; } } return implode( ”, $images ); }
This is the error that came up.
Your PHP code changes were rolled back due to an error on line 67 of file wp-content/themes/generatepress_child/functions.php. Please fix and try saving again. syntax error, unexpected ‘<‘
The error is in this line of code – $images = preg_split(‘/(<img[^>]+\>)/i’, $content, -1, PREG_SPLIT_DELIM_CAPTURE);
Can you help please?
March 14, 2022 at 9:43 pm #2154827Fernando Customer SupportSorry, I missed placing everything in a code block which is causes issues if copied and pasted.
I’ve edited the code I provided above, placing everything in a code block. Can you try it again?
Kindly let us know how it goes. 🙂
March 14, 2022 at 10:18 pm #2154846JodieHi Fernando, that worked, thank you so much!
Cheers Jodie
March 14, 2022 at 10:22 pm #2154848Fernando Customer SupportYou’re welcome Jodie! Glad that worked! Feel free to reach out if you’ll need further assistance with anything else. 🙂
March 20, 2022 at 3:14 pm #2161379JodieHi Fernando,
I want the disclaimer to display on a few pages as well as all my posts. I’ve got it set to display on those pages in the Hook location settings, but they’re not appearing on those pages. Do you know why this might be?
Thanks!
Cheers Jodie
March 20, 2022 at 6:14 pm #2161460Fernando Customer SupportTo make the code work for pages as well rather than only for posts, you would need this code instead:
add_shortcode('portable_hook', function($atts){ ob_start(); $atts = shortcode_atts( array( 'hook2_name' => 'no foo' ), $atts, 'portable_hook' ); do_action($atts['hook2_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 hook2_name="after_first_image"]'); if ( ( is_single() || is_page() ) && ! is_admin() ) { return prefix_insert_after_first_image( $inserted_hook, 1, $content ); } return $content; } function prefix_insert_after_first_image( $insertion, $image_id, $content ) { $images = preg_split('/(<img[^>]+\>)/i', $content, -1, PREG_SPLIT_DELIM_CAPTURE); foreach ($images as $index => $image) { if ( $image_id == $index ) { $images[$index] .= $insertion; } } return implode( '', $images ); }
Moreover, make sure that the Display Rules of your Element is set to show in the specific pages you prefer as well.
Hope this helps! Kindly let us know how it goes. 🙂
March 20, 2022 at 6:39 pm #2161469JodieHi Fernando,
Great, thank you so much! It worked.
-
AuthorPosts
- You must be logged in to reply to this topic.