Support

Account

Home Forums General Issues Checkbox Field, Commas, Shortcode for get_field?

Solved

Checkbox Field, Commas, Shortcode for get_field?

  • I’m hoping someone can help me.

    I’m using ACF (versions 5.7.7) to add content to the bottom of every post. I have a checkbox field which consists of a list of resources which I consistently use in formulating blog posts and when I’m done writing a post, I can check the checkboxes for the resources I used and a “Resources Used For This Article” will appear at the bottom of the post. The field’s Return Value is set to Value. I am using the ACF shortcode within a different plugin’s content (the plugin is Simple Custom Content – https://wordpress.org/plugins/simple-custom-content/).

    That is working great, except that the output of the ACF shortcode is a comma-delimited list of values. I would prefer that the values be separated by line breaks instead of commas.

    I’ve searched through the forums and found several ideas that could prove helpful in my situation:

    https://support.advancedcustomfields.com/forums/topic/xav/
    https://support.advancedcustomfields.com/forums/topic/unwanted-commas-on-frontend/
    https://support.advancedcustomfields.com/forums/topic/multiple-select-field-remove-commas-from-output/
    https://support.advancedcustomfields.com/forums/topic/comma-separated-array-values/
    https://support.advancedcustomfields.com/forums/topic/cpt-archive-need-custom-field-values-as-classes-for-each-post/

    My problem is that I am not experiencing with editing plugins or even with PHP. About the extent of what I can do is take a snippet of code that is given to me and add it to an existing file, as long as someone tells me where exactly to add it (what file and where within the file). The threads I list above seem very promising to me, but all of them appear to be written for people who are much more experienced than I am.

    I think what would be ideal is if I could add code (to functions.php I assume) that would create a shortcode for get_field (which, if I understand the threads above correctly, is necessary to retrieve a list of the checkbox values without commas. That way, I could just use this new shortcode within the Simple Custom Content plugin and only have to do a minimal amount of editing of code.

    Am I on the right track, and if so, how would I create the shortcode for get_field?

    Thanks in advance for any help anyone can offer – it’s much appreciated!

  • I created the following shortcode for you [acfBr field="FIELD_NAME"]. This will get the field and replace all the commas with <br> tags before it returns the result.

    Paste the following code at the bottom of your functions.php document in your theme folder.

    add_shortcode('acfBr', 'brListFunction');
    public function brListFunction($atts, $content) {
    	extract(shortcode_atts(array("id"=>'','field'=>''),$atts));
    	if($field){
    		if($id=''){
    			global $post;
    			$id=$post->ID;
    		}
    		$content=get_field($field,$id);
    		return str_replace(',','<br>',$content);
    	}
        return '';
    }

    This shortcode was created just for your lists and may not work correctly for all your other fields. If you have any issues please let me know.

  • Thank you so much djcottrell! I’ve put your code at the bottom of the functions.php file of my theme and added the shortcode snippet to the Simple Custom Content field.

    Two issues:

    1. At first, I got an error when I tried to save the revised functions.php file: “syntax error, unexpected T_PUBLIC” – associated with the second line of your code. After a quick search online, it seemed like a good first step to remove the “public ” from that line. After doing that, I was able to save the revised functions.php file.

    2. I then added the shortcode snippet to the Simple Custom Content field. But on the front-end of the site, instead of seeing the values from the checkbox field separated by commas, I now see the word “Array”.

    Any thoughts?

    Thanks again – I can’t tell you how much I appreciate your willingness to help me!

    Frank

  • 1. My bad I pulled the function from one of my class files.
    2. I was thinking it was returning a comma seperated list.

    change the following
    return str_replace(',','<br>',$content);
    to
    return implode('<br>',$content);

  • That fixed it. I’ll mark it as solved.

    Thank you, thank you, thank you!

  • Thank you so much for your assistance on this thread. I’ve been pulling my hair out trying to figure this out. For anyone else looking for help paste the snippet below into your functions.php:

    `//Code to display ACF checkboxes as list
    add_shortcode(‘acfBr’, ‘brListFunction’);
    function brListFunction($atts, $content) {
    extract(shortcode_atts(array(“id”=>”,’field’=>”),$atts));
    if($field){
    if($id=”){
    global $post;
    $id=$post->ID;
    }
    $content=get_field($field,$id);
    return implode(‘<br>’,$content);
    }
    return ”;
    }

    Then use the short code:

    [acfBr field="FIELD_NAME"]

  • Thank you so much for your assistance on this thread. I’ve been pulling my hair out trying to figure this out. For anyone else looking for help paste the snippet below into your functions.php:

    //Code to display ACF checkboxes as list
    add_shortcode('acfBr', 'brListFunction');
    function brListFunction($atts, $content) {
    	extract(shortcode_atts(array("id"=>'','field'=>''),$atts));
    	if($field){
    		if($id=''){
    			global $post;
    			$id=$post->ID;
    		}
    		$content=get_field($field,$id);
    		return implode('<br>',$content);
    	}
        return '';
    }

    Then use the short code format to display the appropriate value:

    [acfBr field="FIELD_NAME"]

  • Hello everybody,
    I’m trying this code, and it works.

    what I would like to do though is this:

      <li class=”Value 1″>Value 1
      <li class=”Value 2″>Value 2

    How can I modify the code to get this result?

    Thanks

Viewing 8 posts - 1 through 8 (of 8 total)

The topic ‘Checkbox Field, Commas, Shortcode for get_field?’ is closed to new replies.