Site logo

Archived topic

Add Shortcode between two Posts

5 replies · Started by Patrick on March 1, 2022

Viewing posts 1–6 of 6

Is it possible to add a shortcode between the first and the second Post, without suing WP Show Posts?
Some kind of php snippet that add my shortcode after the first post.

Hi Patrick,

It depends on what's generating the loop for your post listing.

Can you link us to a sample page you want this applied on? So we can inspect the page and figure out the most appropriate insertion point.

While waiting, if you know your way around PHP, you can modify the concept I've done here -
https://generatepress.com/forums/topic/element-after-first-paragraph/#post-1937353

But instead of p, do it with the post item's wrapper element.

Hi, the best example is the marketer theme from your site libary. On the front page I would like to insert a shortcode after the frist post. https://imgur.com/a/dIJuvnk

Hi there,

you can try this PHP Snippet:

add_action('generate_after_do_template_part',function(){
    global $loop_counter;
    $loop_counter++;
    if ( $loop_counter == 1 ) {
        echo do_shortcode('[name_of_shortcode]');
    } 
});

Thank you works great!, I added one filter so it only shows on my homepage

add_action('generate_after_do_template_part',function(){
	if (is_front_page() && is_home() ) {
    global $loop_counter;
    $loop_counter++;
    if ( $loop_counter == 1 ) {
        echo do_shortcode('[custom-twitter-feeds class="twitter-feed" exclude="actions,linkbox,twitterlink"]');
    } 
	}
});

Oh yeah that makes sense :) Glad to be of help!

This archived topic is closed to new replies.