admin管理员组文章数量:1320661
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!
1 Answer
Reset to default 0Okay, I solved my issue - it was too easy.
I replaced
'p' => $bannerid,
with
'p' => $atts['bannerid'],
Now I am happy :)
本文标签: functionsLoad custom post type with ID in a shortcode
版权声明:本文标题:functions - Load custom post type with ID in a shortcode 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742062805a2418664.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论