0

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(
            array(
                'bannerid' => null,            ),
            $atts,
            'banner'
        );       

        $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(); ?>
            <?php 
             }
        } else {
          return 'Nothing found';
        }
        wp_reset_postdata();
    }
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!

0

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

I replaced

'p' => $bannerid,  

with

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

Now I am happy :)

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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