类别属性在自定义快捷代码中不起作用

时间:2017-02-27 作者:Darren Bachan

我编写了以下函数,问题是当我在快捷代码中设置类别时,它会忽略它并显示自定义帖子类型中的所有帖子。所以这只是一种效果。

/*--------------------------------------------------------------
## 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文章来帮助我进行设置。

2 个回复
最合适的回答,由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) // <--------
);
希望这有帮助!

SO网友:rogden

你的$tax_query 需要是税务查询参数数组的数组:

$tax_query = array(
    array(
        \'taxonomy\' => \'resources_categories\',
        \'field\'    => \'slug\',
        \'terms\'    => array( esc_attr($category) ),
        \'operator\' => \'IN\'
    )
);
请参见https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters 了解更多信息。

相关推荐

Namespaced shortcode?

我正在改造一个旧的WP站点,该站点有许多自定义的短代码,显然由于代码当前的组织方式,这些短代码在性能方面付出了代价。当然,我可以修复优化不好的代码,使用十几个短代码,并且一天就可以完成,但我想知道如何更好地组织它们。根据WordPress\'documentation, 建议将它们放在插件中并在上初始化init. 我们可以通过这样“命名”它们来减少这个钩子中的负载吗?[com.company shortcode attr=\"attr\" prop=\"prop\"] 有人尝试过这样的解决方案吗