如何将多个自定义字段作为shortcode的参数传递

时间:2019-08-19 04:28:18

标签: wordpress plugins shortcode

我正在学习如何在shorcode中传递参数,我了解了WP Codex的基本步骤。

但是现在,我有带有自定义字段的插件,并且标题是我想知道如何做。

这是插件中添加自定义字段的代码

$prefix = '_al_listing_';
$fields = [];

$fields[] = [
    'name' => __( 'Price', 'auto-listings' ),
    'id'   => $prefix . 'price',
    'type' => 'number',
    'min'  => 0,
    'step' => '0.01',
];
$fields[] = [
    'name' => __( 'Suffix', 'auto-listings' ),
    'desc' => __( 'Optional text after price.', 'auto-listings' ),
    'id'   => $prefix . 'price_suffix',
    'type' => 'text',
];

$fields = apply_filters( 'auto_listings_metabox_details', $fields );

ksort( $fields );

return [
    'id'         => $prefix . 'details',
    'title'      => __( 'Details', 'auto-listings' ),
    'post_types' => 'auto-listing',
    'fields'     => $fields,
    'context'    => 'side',
];

这是我用来调用带参数的简码的代码,但我一次只知道传递1个自定义字段。

public function listings( $atts ) {
    $atts = shortcode_atts(
        [
            'orderby' => 'date',
            'order'   => 'asc',
            'number'  => '20',
            'price' => ''
        ],
        $atts
    );

    $query_args = [
        'post_type'           => $post-type,
        'post_status'         => 'publish',
        'meta_key'            => '_al_listing_price',
        'orderby'             => 'meta_value',
        'meta_value' => $atts['price'],
        'order'               => $atts['order'],                
        'posts_per_page'      => $atts['number'],
    ];

    return $this->listing_loop( $query_args, $atts, 'listings' );
}

感谢您的帮助。

0 个答案:

没有答案
相关问题