如何能够使用一个空白的短码参数来显示所有元宝盒的值,而不是没有。

问题描述 投票:0回答:1

我使用的是我开发的一个Wordpress自定义短码,查询内容如下。

 $atts = shortcode_atts( array( 'teamtype' => '' ), $atts );

    $args = array(
        'post_type'      => 'team',
        'posts_per_page' => '10',
        'publish_status' => 'published',
            'meta_query'       => array(
        'relation'    => 'AND',
        array(
            'key'          => 'my_key',
            'value'        => $atts['teamtype'],
            'compare'      => '=',
        ),

        array(
            'key'          => 'meta-featured_checkbox',
            'value'        => 'yes',
            'compare'      => '=',
        )
    ),
    );

它查询一个名为 "my_key "的自定义元键,因此该短码使用的参数是这样的。[shortcodename teamtype="type1"]

我希望能够将该参数留空,以显示my_key元键的所有值,如[shortcodename teamtype=""] 。

当我使用如下参数时,不使用元查询数组,我可以把参数留空,全部显示出来。

$args = array(
                    'post_type'      => 'team',
                    'posts_per_page' => '10',
                    'publish_status' => 'published',
        'meta_key'       => 'my_key',
        'meta_value'     => $atts['teamtype']
                 );

有谁知道为什么上面的参数不允许我这么做?以及如何让它工作?

谢谢大家

php wordpress shortcode
1个回答
0
投票

我设法让它工作...!

在meta_query数组中,对于短码中的参数,我用 "meta_key "代替 "key",用 "meta_value "代替 "value"。

诸如:

$args = array(
        'post_type'      => 'team',
        'posts_per_page' => '10',
        'publish_status' => 'published',
            'meta_query'       => array(
        'relation'    => 'AND',
        array(
            'meta_key'          => 'my_key',
            'meta_value'        => $atts['teamtype'],
            'compare'      => '=',
        ),

        array(
            'key'          => 'meta-featured_checkbox',
            'value'        => 'yes',
            'compare'      => '=',
        )
    ),
    );
© www.soinside.com 2019 - 2024. All rights reserved.