最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

functions - Load custom post type with ID in a shortcode

programmeradmin0浏览0评论

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(
            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 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!

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(
            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 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!

Share Improve this question edited Jun 10, 2019 at 17:09 RyanS 1256 bronze badges asked Nov 8, 2018 at 18:26 FrancisFrancis 35 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

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

I replaced

'p' => $bannerid,  

with

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

Now I am happy :)

发布评论

评论列表(0)

  1. 暂无评论