- This topic has 7 replies, 2 voices, and was last updated 3 years, 3 months ago by
webmasterla-montgolfiere-fr.
-
AuthorPosts
-
February 6, 2023 at 4:01 am #2523037
webmasterla-montgolfiere-fr
Hi to the team !
Many thanks again for your work and I’m really satisfied with Generatepress ๐ Contratulations !
I read this really interesting post and works fine :
https://generatepress.com/forums/topic/decrease-the-spacing-between-the-headline-and-image/#post-1672071I would like to know if there is a way to :
– Insert the featured image automatically before the last H2 in the content
– and with the caption.Many thanks again !
February 6, 2023 at 7:37 am #2523210David
StaffCustomer SupportHi there,
injecting ‘code’ into the content using the
the_contentfilter hook can get really complicated.
Does it have to be before the LAST H2 ?February 6, 2023 at 8:06 am #2523366webmasterla-montgolfiere-fr
Ah ok I understand.
Does it have to be before the LAST H2 ?
It’s definitely the best option for the reader and for consistency and hierarchical logic.
Hope it’s possible, if not I have to do manually for 800 contents ๐
February 6, 2023 at 8:07 am #2523367webmasterla-montgolfiere-fr
I sent my message to much fast …
Just forgot: many thanks David !
February 6, 2023 at 8:32 am #2523392David
StaffCustomer SupportTry this method:
function featured_image_after_last_h2($content) { // abort if not single post or content has no H2 if ( ( !is_singular('post') ) || ( strpos($content, '<h2') === FALSE ) ) { return $content; } // get the post thumbnail and caption global $post; $feat_img = get_the_post_thumbnail($post->ID, 'post-single'); $feat_caption = get_the_post_thumbnail_caption($post->ID); // set the counter target to last H2 $heading_count = substr_count($content, '<h2>'); // loop through content and insert the HTML $content = explode("<h2", $content); $new_content = ''; for ($i = 0; $i < count($content); $i++) { if ($i == $heading_count) { $new_content .= '<figure class="incontent-featured-image">' . $feat_img . '<figcaption>' . $feat_caption . '</figure>'; } if($i>0) { $new_content.= "<h2".$content[$i]; } else { $new_content.= $content[$i]; } } return $new_content; } add_filter('the_content', 'featured_image_after_last_h2');February 6, 2023 at 11:14 am #2523560webmasterla-montgolfiere-fr
Wow it works like a charm ๐
It’s so nice to give me the solution ! Hope it took not so much time and could help others users ๐
Great team for a great theme ๐Many thanks again David !
February 7, 2023 at 2:03 am #2524162David
StaffCustomer SupportFor reference and kudos, i found the code for
before H2here:https://stackoverflow.com/q/61668699
And then revised that to find the last H2 to insert before ๐
Glad to hear it is working!
February 7, 2023 at 4:40 am #2524314webmasterla-montgolfiere-fr
Great to link to the code ๐
๐
Many thanks again !
Have a great day ! -
AuthorPosts
- You must be logged in to reply to this topic.