Display Post from Custom Post Type with Shortcode

  • You enter second parameter in add shorcode is incorrect,you missing callable function in parameter.
  • Like below this add_shortcode with parameter.

add_shortcode( string $tag, callable $callback );

Code :

function cpt_content_func($atts){
    $post="";
    $content="";
    extract( shortcode_atts( array(
        'slug' => null,
    ), $atts ) );
    $args = array(
          'post_type' => 'location', 
          'posts_per_page' => 1, 
          'post_name__in' => $atts['slug'] ); 

    $post = get_posts( $args );
    if ( !empty( $post ) ) {
        $content = $post[0]->post_content;
    }
    return $content;
}
add_shortcode('stefan_location','cpt_content_func');