我已经为我的网站上的位置创建了一个自定义帖子类型,我想用短代码完整地显示每个帖子。我对创建短代码很陌生,我正努力想办法解决它。我想按slug这样显示帖子:[stefan\\u location slug=“Aspley”]但我无法让它工作。我知道我可能离得太远了,所以欢迎任何建议!!
function cpt_content_func($atts){
$post = \'\';
$content = \'\';
extract( shortcode_atts( array(
\'slug\' => null,
), $atts ) );
$args = array(
\'name\' => $slug,
\'post_type\' => \'location\',
\'numberposts\' => 1
);
$post = get_posts( $args );
if ( !empty( $post ) ) {
$content = $post[0]->post_content;
}
return $content;
}
add_shortcode(\'stefan_location\',\'stefan_location\');
SO网友:HK89
您在add shorcode中输入的第二个参数不正确,参数中缺少可调用函数
如下所示,此add\\u shortcode带有参数添加\\u短代码(字符串$标记,可调用$回调);
代码:
function cpt_content_func($atts){
$post = \'\';
$content = \'\';
extract( shortcode_atts( array(
\'slug\' => null,
), $atts ) );
$args = array(
\'post_type\' => \'location\',
\'posts_per_page\' => 1,
\'post_name__in\' => $atts[\'slug\'] );
$post = get_posts( $args );
if ( !empty( $post ) ) {
$content = $post[0]->post_content;
}
return $content;
}
add_shortcode(\'stefan_location\',\'cpt_content_func\');