I developed 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 by typing 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!


Okay, I solved my issue - it was too easy.

I replaced

'p' => $bannerid,  


'p' => $atts['bannerid'],  

Now I am happy :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.