我编写了以下函数,问题是当我在快捷代码中设置类别时,它会忽略它并显示自定义帖子类型中的所有帖子。所以这只是一种效果。
/*--------------------------------------------------------------
## Resources Shortcode
--------------------------------------------------------------*/
function resources_query( $atts ) {
extract(shortcode_atts(array(
\'category\' => \'\',
\'per_page\' => -1,
\'orderby\' => \'date\',
\'order\' => \'ASC\'
), $atts));
$tax_query = array(
\'taxonomy\' => \'resources_categories\',
\'field\' => \'slug\',
\'terms\' => array( esc_attr($category) ),
\'operator\' => \'IN\',
);
$args = array(
\'post_type\' => \'resources\',
\'post_status\' => \'publish\',
\'posts_per_page\' => $per_page,
\'orderby\' => $orderby,
\'order\' => $order,
\'tax_query\' => $tax_query
);
$resources_query = new WP_Query( $args );
if ( $resources_query->have_posts() ) :
$html_out = \'<div class="fg-row row flex-row">\';
while ( $resources_query->have_posts() ) :
$resources_query->the_post();
$title = get_the_title();
$content = get_the_excerpt();
$pdf = get_field( "download_pdf" );
// Do stuff with each post here
$html_out .= \'<div class="fg-col col-xs-12 col-md-4 fg-text-dark"><section class="icon-box-v3 light-box text-left fg-text-dark"><i class="icon-box-v3-icons ff-font-et-line icon-document fg-text-dark ffb-icon-1"></i>\';
$html_out .= \'<h3 class="icon-box-v3-title fg-text-dark ffb-title-2">\' . $title . \'</h3><p class="icon-box-v3-title-paragraph fg-text-dark ffb-description-3">\' . $content . \'</p>\';
if( $pdf ):
$html_out .= \'<a href="\' . $pdf . \'" class="ffb-block-button-1-0 ffb-btn ffb-btn-v1 ffb-btn-link btn-base-brd-slide btn-slide btn-base-md btn-w-auto fg-text-dark ffb-button1-1 btn-light-box" target="_blank"><span class="btn-text">Download PDF</span></a>\';
endif;
$html_out .= \'</section></div>\';
endwhile;
$html_out .= \'</div>\';
else : // No results
$html_out = "No Resources Found.";
endif;
wp_reset_query();
return $html_out;
}
add_shortcode( \'show_resources\', \'resources_query\' );
下面是我的短代码的实际外观
[show_resources category="post-surgery-information" per_page="-1"]
. 我不知道为什么它会忽略它并显示所有帖子。我查看了其他Stack文章来帮助我进行设置。
最合适的回答,由SO网友:nibnut 整理而成
因此,我认为有两件事可能导致这个问题:
我真的怀疑你是否需要esc_attr
用于查询
$tax_query = array(
\'taxonomy\' => \'resources_categories\',
\'field\' => \'slug\',
\'terms\' => array( $category ), // <--------
\'operator\' => \'IN\',
);
$args = array(
\'post_type\' => \'resources\',
\'post_status\' => \'publish\',
\'posts_per_page\' => $per_page,
\'orderby\' => $orderby,
\'order\' => $order,
\'tax_query\' => array($tax_query) // <--------
);
希望这有帮助!