您可以使用shortcode_atts
获取您正在传递的类别[home_slider cat=1]
. 我想你的分类法是category
. 您可以替换为您的分类法。您还可以使用逗号分隔的方式传递多个cat id。对于eg[home_slider cat=1,2,3]
. 它将返回该类别帖子的滑块。
function home_slider($params = array()){
extract(shortcode_atts(array(
\'cat\' => "",
), $params));
ob_start();
$args = array(
\'post_type\' => \'productslider\',
\'order\' => \'DESC\',
\'orderby\' => \'date\',
\'posts_per_page\' => 3,
\'post_status\' => array(\'publish\'),
);
if(!empty($cat))
{
if( strpos($cat, \',\' ) !== false ) {
$cat = explode( \',\' , $cat );
}
$args[\'tax_query\'] = array(
array(
\'taxonomy\' => \'category\',
\'field\' => \'term_id\',
\'terms\' => $cat
)
);
}
$hm_sliders = get_posts( $args );?>
<div class="cp-heroWrapper">
<div class="slideshow js-slideshow cp_heroSlider">
<?php
foreach($hm_sliders as $hm_slider){
$feat_image = wp_get_attachment_url( get_post_thumbnail_id($hm_slider->ID) );?>
<div class="slide">
<div class="slide_img">
<div class="cp-heroBannerImg">
<a href="<?php echo get_post_meta($hm_slider->ID, \'imageURlLink\', true );?>" title=""><img src="<?php echo $feat_image; ?>"><div class="bg_overlay"></div></a>
</div>
<div class="cp-heading">
<h2><a href="<?php echo get_post_meta($hm_slider->ID, \'imageURlLink\', true );?>" title=""><?php echo $hm_slider->post_title ?></a></h2>
</div>
</div>
</div>
<?php } ?>
</div>
</div>
<?php
return ob_get_clean();
}
add_shortcode( \'home_slider\', \'home_slider\' );