Can I wrap an unordered list inside a shortcode?

by user49624   Last Updated January 11, 2019 12:08 PM

So, I created an events page for my client that holds lots of details about events. We found no particular event plugins to be helpful for their purpose, so I built a a custom template using advanced custom fields. They are very computer illiterate and will need to be entering these events in very frequently. I used a few shortcodes from my theme to create the layout for these pages. Now I want to take basically make the page super simple for them to fill out, like a form, and output the same layout. To so this, I need to code the shortcodes into my template. All of the shortcodes have opening & closing tags, like this:

[one-half] content [/one-half]

I want to put this list (which includes my advanced custom fields), inside the shortcode tags:

<ul class="strong">

<li><?php the_field('highlights_list_item#1'); ?></li>

<li><?php the_field('highlights_list_item#2'); ?></li>

</ul>

All I have found so far (that is helpful) for putting content inside a shortcode, is this:

<?php echo do_shortcode('[one_half]'.$text_to_be_wrapped_in_shortcode.'[/one_half]'); ?>

But how do I put a list inside there? Can I have advanced custom fields inside shortcode tags?

I thought about just styling this to match what the shortcode does, but I'd like to learn this. Also, I have another section that I will have to do the same thing with, but it is a toggle with an advanced custom field paragraph inside of it.

I really appreciate the help!!



Answers 1


You can capture the list in a variable, rather than echo it:

$list =
    '<ul class="strong">
        <li>' . get_field( 'highlights_list_item#1' ) . '</li>
        <li>' . get_field( 'highlights_list_item#2' ) . '</li>
    </ul>';

echo do_shortcode( '[one_half]' . $list . '[/one_half]' );

Note the use of get_field() rather than the_field(), which returns a custom field value as opposed to displaying it.

TheDeadMedic
TheDeadMedic
March 28, 2014 15:26 PM

Related Questions



Get a list of ACF Repeater-Fields as array

Updated February 25, 2016 03:03 AM

Show taxonomy list with taxonomy images

Updated May 27, 2019 11:08 AM

Show taxonomy list with taxonomy ACF images

Updated May 27, 2019 17:08 PM