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

时间:2019-08-19 作者:Hung PD

我正在学习如何在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\',
];
这是我构建的用参数调用shortcode的代码,但我只知道一次传递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\' );
}
感谢您的帮助。

1 个回复
SO网友:Rudy Lister

您可以用几种不同的方法来实现这一点。需要知道如何在当前快捷码中传递1自定义字段。

看起来你可能是这样做的:

[your_short_code price="how_are_you_setting_this"]
如果是这种情况,则可以添加更多属性,如下所示:

[your_short_code price="how_are_you_setting_this" next_field="set_this_field"]

// From your code above with adjustments.
public function listings( $atts ) {
global $post;
// You can add on to the default atts.
    $atts = shortcode_atts(
        [
            \'orderby\' => \'date\',
            \'order\'   => \'asc\',
            \'number\'  => \'20\',
            \'price\' => \'\',
            \'next_field\' =>\'\',
        ],
        $atts
    );

// Not exactly sure what you are trying to do here.
    $query_args = [
        \'post_type\'           => $post->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\' );
}
另一种方法是:

// notice that the attribute fields has multiple fields separated by commas.
[your_short_code fields="how_are_you_setting_this,set_next_field,and_set_next"]
如果使用分隔的逗号,那么在php端的短代码中,需要执行以下操作:

// From your code above with adjustments.
    public function listings( $atts ) {
    global $post;
    // You can add on to the default atts.
        $atts = shortcode_atts(
            [
                \'orderby\' => \'date\',
                \'order\'   => \'asc\',
                \'number\'  => \'20\',
                \'fields\' => \'\',
            ],
            $atts
        );

        // This will make an array out of your comma separated fields.
        $fields = explode( \',\', $atts[\'fields\'] );

    // Not exactly sure what you are trying to do here.
        $query_args = [
            \'post_type\'           => $post->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\' );
    }
希望这对你有帮助。

相关推荐

允许在自定义Metabox区域中使用HTML

我知道有很多问题与我的问题有关,但我真的无法得到解决。我的问题很简单,如何允许post上自定义metabox中的textarea中的html。到目前为止,我已经创建了添加元框的代码。add_action( \'add_meta_boxes_post\', function ( $post ) { if ( $post->_wp_page_template === \'page-templates/skyscraper-post.php\' ) { add_me