I'm learning how to pass parameters in shorcode, I know the basic step after read through WP Codex.
But for now, I have plugin come with custom fields and as the title, I want to know how to do it.
Here is the code from plugin that adds custom fields
$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',
];
And here is the code I built to call shortcode with parameters, but I only know pass 1 custom fields at one time.
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' );
}
Any helps are appreciated.
I'm learning how to pass parameters in shorcode, I know the basic step after read through WP Codex.
But for now, I have plugin come with custom fields and as the title, I want to know how to do it.
Here is the code from plugin that adds custom fields
$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',
];
And here is the code I built to call shortcode with parameters, but I only know pass 1 custom fields at one time.
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' );
}
Any helps are appreciated.
Share Improve this question asked Aug 19, 2019 at 2:43 Hung PDHung PD 2972 gold badges7 silver badges15 bronze badges1 Answer
Reset to default 0You can do this several different ways. Need to know how you are passing the 1 custom field in your current shortcode.
It seems like you might be doing it like this:
[your_short_code price="how_are_you_setting_this"]
if this is the case then you can just add more attributes like 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' );
}
Another way of doing it is like this:
// 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"]
If you go with the separated commas then in your shortcode on the php side you will need to do as follows:
// 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' );
}
Hope this helps you.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745233054a4617765.html
评论列表(0条)