Get choices of a select field
Support › MB Builder › Get choices of a select fieldResolved
- This topic has 6 replies, 2 voices, and was last updated 1 year, 8 months ago by
Reisecoupon.
-
AuthorPosts
-
July 23, 2023 at 3:34 AM #42721
Reisecoupon
ParticipantHi,
how can I get the choices of a select field? For example, I have a list (id: mylist) and 3 choices (red, blue, yellow) and I want to list them on the front end.
Thank you for your help!
July 23, 2023 at 8:58 PM #42725Peter
ModeratorHello,
If you want to output the selected choices, please follow the documentation https://docs.metabox.io/fields/select/#template-usage
If you want to output all supported choices, please use the function
rwmb_get_field_settings()
. Refer to the documentation https://docs.metabox.io/functions/rwmb-get-field-settings/August 1, 2023 at 1:36 AM #42789Reisecoupon
ParticipantHi,
I have the custom post type "listing", related to this custom post type I try to use this code to show all available choices of my select custom field "city":
<?php
$field = rwmb_get_field_settings( 'city' );
echo ($field);
?>Nothing happens.
What did I wrong?
Thank you for your help!
August 1, 2023 at 5:49 PM #42791Peter
ModeratorHello,
You should add the code to the template of the listing post to get the current post ID. If not you need to pass the post ID to the function. And the return value is an array so you need to use the function
var_dump()
orprint_r()
to print out the value.For example:
$field = rwmb_get_field_settings( 'city', '', 123 ); // 123 is the post ID echo '<pre>'; print_r( $field ); echo '</pre>';
August 2, 2023 at 12:25 AM #42793Reisecoupon
ParticipantThank you, but I would like to show (echo) all available choices of this field "city", not the selected choice of a random related custom post.
For example, my field "city" has 3 choices:
- Barcelona
- Madrid
- LondonAnd I simple want to echo these fields.
Thank you!
August 2, 2023 at 9:17 PM #42797Peter
ModeratorHello,
Use the function
print_r()
to know the data structure of the field settings. You will need to have a basic knowledge of coding to echo the choice. For example:$field = rwmb_get_field_settings( 'city', '', 123 ); // 123 is the post ID $field_choices = $field['options']; foreach ($field_choices as $value => $label) { echo "Value: " . $value; echo "<br />"; echo "Label: " . $label; echo "<br />"; }
If you are not able to complete the task, please contact us here https://metabox.io/contact/
our development team will help you with an extra fee.August 3, 2023 at 2:11 AM #42802Reisecoupon
ParticipantHi, thanks, it works, but what happens if I delete this post? The code will not work.
The problem is that the post isn´t a permanent content on the site, it has an expiration and after that I delete it.
I tried to use the field group id (because the field group is permanent, I wouldn´t delete it), but it does not work woth it.
Isn´t any better solution?
Thank you!
-
AuthorPosts
- You must be logged in to reply to this topic.