所以,我对这个短代码有点陌生,但我现在几乎已经完成了我需要的工作。(检查下面的代码)
我想在我的短代码中过滤我的内容,我有一个由ACF插件添加字段的CPT。现在我需要的是,当我放置[speaker\\u overview\\u 2017 year=2017]时,它只显示2017年有价值的项目,当我做[speaker\\u overview\\u 2017 year=2018]时,它只显示2018年,当我做[speaker\\u overview\\u 2017]时,它显示所有项目。
这是我的短代码,有人能帮我一下吗?
add_shortcode(\'speaker_overview_2017\', \'speakers_overview_shortcode\');
function speakers_overview_shortcode($atts){
$args = array(
\'post_type\' => \'qp_speakers\',
\'post_status\' => \'publish\',
\'orderby\' => \'menu_order\',
\'order\' => \'ASC\',
\'posts_per_page\' => -1,
\'meta_query\' => array(
array(
\'key\' => \'speakers_jaar\',
\'value\' => \'2017\',
)
)
);
ob_start();
$locaties = new WP_Query( $args );
if ( $locaties->have_posts() ) : ?>
<?php while ( $locaties->have_posts() ) : $locaties->the_post(); ?>
<?php q2_speakers_template(); ?>
<?php endwhile; // end of the loop. ?>
<?php endif;
wp_reset_postdata();
return ob_get_clean();
}
最合适的回答,由SO网友:obiPlabon 整理而成
添加到快捷代码的年份属性[speaker_overview_2017 year=2017]
将通过$atts
以这种方式$atts[\'year\']
. 我想,你想做一个元查询,关键是speakers_jaar
值为$atts[\'year\']
. 请检查以下代码如果在快捷码中设置了年份,则查询将限于该年份,否则不受限制
function speakers_overview_shortcode( $atts ) {
$atts = shortcode_atts( array(
\'year\' => \'\'
), $atts );
$args = array(
\'post_type\' => \'qp_speakers\',
\'post_status\' => \'publish\',
\'orderby\' => \'menu_order\',
\'order\' => \'ASC\',
\'posts_per_page\' => -1,
);
if ( ! empty( $atts[\'year\'] ) ) {
$args[\'meta_query\'] = array(
array(
\'key\' => \'speakers_jaar\',
\'value\' => absint( $atts[\'year\'] ),
)
);
}
$locaties = new WP_Query( $args );
ob_start();
if ( $locaties->have_posts() ) :
while ( $locaties->have_posts() ) {
$locaties->the_post();
q2_speakers_template();
}
}
wp_reset_postdata();
return ob_get_clean();
}
add_shortcode( \'speaker_overview_2017\', \'speakers_overview_shortcode\' );