Search code examples
wordpressshortcode

How to call a shortcode in each post in the main blog post listing page?


I'm relatively new here. I tried to execute a shortcode which is [ssba] in every single post in their main listing page (please check the page http://www.theevidencenetwork.com.php54-1.ord1-1.websitetestlink.com/news-events) but was unable to do so. When I view each post in their own page like when I click on any post listed there and view them the shortcode works just fine. But I want to show them in the main page as well.

How do I do that?


Solution

  • I'm relatively new here too..

    The shortcode is probably not showing because it's the_excerpt(), it doesn't render shortcodes or html.

    There are a lot of ways to solve this 'problem'

    I think you can try this:

    add_filter( 'the_content', 'ssba_the_content_filter' );
    
    function ssba_the_content_filter( $content ) {
    
        $new_content = $content;
    
        $new_content .= do_shortcode( '[ssba]' );
    
        return $new_content;
    }
    

    Put this code on your functions.php

    This will automatically add the [ssba] shortcode at the end of every the_content() and the_excerpt() in your theme. With this solution you don't need to manually eneter this information on every post

    if you want you can use conditional tags inside so you add only at the pages you want.

    add_filter( 'the_content', 'ssba_the_content_filter' );
    
    function ssba_the_content_filter( $content ) {
    
        $new_content = $content;
    
        if( is_single() ) {
            $new_content .= do_shortcode( '[ssba]' );
        }
    
        return $new_content;
    }
    

    or like AgmLauncher said, you can use do_shortcode()in the loop.

    if ( have_posts() ) {
        while ( have_posts() ) {
            the_post(); 
    
            echo '<div>';
                the_content();
                echo do_shortcode('[ssba]');
            echo '</div>';
        } // end while
    } // end if
    

    sorry for bad english.