Load custom post type with ID in a shortcode

I develop a theme for the first time. I created a custom post type for banners/call to actions that I want to include in different posts with a shortcode. The shortcode should look like [banner id=123].

Until now, all banners are shown, and the parameter of my shortcode doesn’t filter the results. Where is my failure?

function mrt_shortcode_banner( $atts ) {
// Attributes
$atts = shortcode_atts(
‘bannerid’ => null, ),

$mrt_load_banner = new WP_Query( array(
‘post_type’ => ‘banner’,
‘p’ => $bannerid ,
) );

if ( $mrt_load_banner->have_posts() ) {
while ( $mrt_load_banner->have_posts() ) { $mrt_load_banner->the_post(); ?>
<?php the_content(); ?>
} else {
return ‘Nothing found’;
add_shortcode( ‘banner’, ‘mrt_shortcode_banner’ );

I have tested to type the ID as a numeric value in the function (‘p’ => 123). This worked fine. So I guess I have to specify the bannerid in another way. But I don’t know how. Thanks for your hints!

Read more here:: Load custom post type with ID in a shortcode

Leave a Reply

Your email address will not be published. Required fields are marked *