• Resolved pappo87

    (@pappo87)


    Hello, thank you for your awesome plugin.
    I need to extract all values from radio buttons field created with advanced custom fields.
    I have a city list and I’m using global variables to display matching posts with ?city=name in the url.
    Now I need to display the whole city list on the main page, but only if a post containing the city exists.
    In php it can be done with <?php echo implode(', ', get_field_object('example')[choices]); ?> and this code doesn’t work in page, only in single post.
    Then I would like to pass each of those values through a loop and if posts are found, show a link on the page.
    I think using nested loop would be nice, one to loop each extracted radio button value and one to loop through posts.

    [loop category=cities]
    [-loop (code to loop the checkbox array here)]
    [for each=city_field value='{checkbox_value}’]
    [each title]

    [/for]
    [-loop]
    [/loop]

    I know it’s a strange request, but there is a way to do it with your plugin?
    Thanks in advance
    Tony

    https://www.ads-software.com/plugins/custom-content-shortcode/

Viewing 9 replies - 1 through 9 (of 9 total)
  • Thread Starter pappo87

    (@pappo87)

    I finally extracted all radio button fields on my page.
    You can do it typing the field key instead of field name in the code <?php echo implode(', ', get_field_object('example')[choices]); ?>
    The result is an array so it can be further processed if needed.
    With the Shortcode exec php I embedded a php snippet in my page. I was wondering if custom content shortcode had a snippet to extract an array of this kind of field.

    Plugin Author Eliot Akira

    (@miyarakira)

    Hello,

    I looked into this, and the solution is specific to ACF – as you described, to use the get_field_object function.

    It sounds like a useful function to loop through available options for a checkbox/radio/select field. In the newest update, I added a parameter to the [array] shortcode to support this.

    [array choices=checkbox_field]
      Option label: [field label] or {LABEL}
      Option value: [field value] or {VALUE}
    [/array]

    So, I hope you can use this to achieve what you need.

    Thread Starter pappo87

    (@pappo87)

    Thank you for your update. I tried your code but unfortunately it returns a php warning: Warning: Invalid argument supplied for foreach() in /public_html/wp-content/plugins/custom-content-shortcode/includes/core/content.php on line 1853
    There must be problems retrieving these kind of values on page. It returns the same error with field name or field key.
    Thanks in advance

    Plugin Author Eliot Akira

    (@miyarakira)

    Hmm, I’m not seeing the warning on my end, with any checkbox/select/radio fields.

    OK, I added a way to debug the content of the array. Could you download the newest version 2.4.7 and tell me the output of the following?

    [array choices=radio_field debug=true][/array]

    There should be an array key called choices.

    Thread Starter pappo87

    (@pappo87)

    Maybe before I did something wrong, but now it’s working fine.
    Now I’ll find a way to loop each array value and output a link if a post matching the value is found.
    Thank you very much, very appreciate your kindness.

    Plugin Author Eliot Akira

    (@miyarakira)

    Glad to hear it’s working. From what you described, I think you want something like this:

    [array choices=checkbox_field]
      [loop type=post taxonomy=cities term='{VALUE}']
        [if first] City Name: {LABEL} [/if]
        [field title]
        ...
      [/loop]
    [/array]

    This should go through available choices for the field one by one, and if there are matching post(s) it will display a title and list the posts. If no matching post, it will display nothing.

    Thread Starter pappo87

    (@pappo87)

    Modifying your code to work in my site, I got this:

    [array choices=field_55a4468691fa7]
      [loop category=restaurants field=city value='{VALUE}']
        [if first] <p>City: {VALUE} </p> [/if]
      [/loop]
    [/array]

    where field is a custom field.
    It’s working, but it also outputs lots of <br> tags between the two values and the page become so long! Any way to avoid this?

    Plugin Author Eliot Akira

    (@miyarakira)

    This is due to auto-formatting of post content which converts new lines to <br>. The quickest solution is to go to Settings and enable [raw] shortcode, then wrap the block:

    [raw]
      ..your code here..
    [/raw]

    This prevents formatting of what’s inside it.

    Other solutions include putting the whole code in one line, or loading it from a custom field or post type.

    Thread Starter pappo87

    (@pappo87)

    This worked perfectly! Thanks to your plugin I can completely avoid php use.
    Thank you very much
    Tony

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘getting all radio button values’ is closed to new replies.