0

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

enter image description here

...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!

3
  • Note that most people don't use this interface and would either build a metabox if using the classic editor, or use a block if using the modern editor, or a feature or a 3rd party plugin
    – Tom J Nowell
    Commented Apr 29, 2022 at 16:58
  • I get it but this feature is still alive and active for plugins that import and export content via .csv files for WordPress. Doing bulk uploads of posts with custom content. I don't think there is a builk .csv plugin that allows us to import multiple blocks per post, unless we're using ACF Pro
    – klewis
    Commented Apr 29, 2022 at 17:14
  • 1
    that doesn't prevent you building a user interface as long as it stores the same way, but if you're dealing with a plugin then most answers you get will probably break compatibility if they try to change how the data is stored, so will storing multiple fields with the same name, you'll probably find the plugin only recognises the very first field defined
    – Tom J Nowell
    Commented Apr 29, 2022 at 17:28

1 Answer 1

3

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
1
  • Thanks Tom. I owe you steak! Will try this out shortly.
    – klewis
    Commented Apr 29, 2022 at 17:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.