Custom Fields - How to create a list from multi-line entries of a single value

If I have the following name/value pair for a custom field....

...is there a PHP loop process I can apply that will allow me to display them on the front-end as individual values because there is a line-break between each item?

or do I have to create multiple name/value pairs for each item?

The end goal in my case is to create a list on the front end of these items that are being pulled from this value area.

Thanks for any tips!

Topic loop custom-field Wordpress

Category Web


Yes, grab the value via get_post_meta, then it's just a matter of generic PHP loops and string manipulation.

Try using the explode function with \n as the separator to create an array you can loop through. You may need to check for empty values since some of your lines are blank, but the get_post_meta part is the only part of this question that requires WordPress knowledge:

$data = get_post_meta( $post_id, 'What Responsibilities Will I have?', true );
$lines = explode( '\n', $data );

// now do things with the $lines array

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.