• Hello everyone,
    I’ve managed to create a custom post plugin and now I am trying to display the data using custom shortcode.
    I understand that shortcodes work with return instead of echo/print. I am just wondering if anyone knows why the content gets displayed above the div I’ve put it in. It only happens when I display data from the database ( the_content() ). If the $output is only a string that I’ve typed manually, it works. What is wrong with the code?

    Any help is much appreciated!

    //[sustainability-plugin]
    function sus_func( $atts ){
    
        $output = '';
        $args = array( 'post_type' => 'sustainability_post', 'posts_per_page' => 10 );
        $loop = new WP_Query( $args );
    
        $output .= '<div class="wrapper">';
        while ( $loop->have_posts() ) : $loop->the_post();
        $output .= '<div class="content">'. the_content() .'</div>';
        endwhile;
        $output .= '</div>';
    
        return $output;
    
    }
    add_shortcode( 'sustainability-plugin', 'sus_func' );
Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    the_content() has it’s own echo statement, it doesn’t return anything. In this context you want to use get_content()

    Thread Starter Rad199

    (@rad199)

    Using get_content() brakes the page… Any other ideas?

    Thread Starter Rad199

    (@rad199)

    Finally managed to resolve the issue. I used

    $post_title = get_the_title($post->ID)

    and that does the job 🙂
    I hope that this will help someone else too.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Shortcode rendering above div that is put in’ is closed to new replies.