简体   繁体   English

按类别WordPress过滤自定义帖子类型短代码输出

[英]Filter custom post type shortcode output by category WordPress

I've created a custom post type for a "Clubs" section on a site, and I need to filter those clubs by their categories, using a shortcode. 我为网站上的“俱乐部”部分创建了一个自定义帖子类型,我需要使用短代码按类别过滤这些俱乐部。

Not entirely sure why the shortcode isn't working to filter the clubs by category (it does display all clubs). 不完全确定为什么短代码不能按类别过滤俱乐部(它确实显示所有俱乐部)。

For example, if one of the categories is AV Clubs, I've tried using both the title [club category="AV Clubs"] and the slug [club category="av-clubs"]. 例如,如果其中一个类别是AV俱乐部,我尝试使用标题[club category =“AV Clubs”]和slug [club category =“av-clubs”]。

// Shortcode
function shortcode_club_viusu( $atts ) {
//default arguments
$args = shortcode_atts( array(
    'cat' => null,
    'category' => '',
    'order' => 'DESC',
    'orderby' => 'date',
    'posts_per_page'=> -1,
    'post_type' => 'club'
), $atts );

$content = "";

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {
    //beginning of the FAQ element
    $content .= '<ul class="collapsible" data-collapsible="accordion">';

    while ( $the_query->have_posts() ) {
        $the_query->the_post();

        $content .= '<li>';
        $content .= '<div class="collapsible-header">'.get_the_title().'</div>';
        $content .= '<div class="collapsible-body"><p>'.get_the_content().'</p></div>';
        $content .= '</li>';
    }

    //closing the FAQ element
    $content .= '</ul>';

} else {
    $content .= "No Clubs found";
}
/* Restore original Post Data */
wp_reset_postdata();

return $content;
}
add_shortcode( 'club', 'shortcode_club_viusu' );

Turns out I was missing one line from the list above: 结果我错过了上面列表中的一行:

'cat' => null,
'club_tax' => '',
'category' => '',
'order' => 'DESC',
'orderby' => 'date',
'posts_per_page'=> -1,
'post_type' => 'club'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM