问题
I have a custom field who's content I would like to output as a ul list.
The custom field contains words that are separated with spaces.
I'm trying to use this code here but it's not working.
<?php
$list_items = get_post_meta($post->ID, 'idid');
if($list_items){
$list_items = explode(" ", $list_items) {
echo '<ul>';
foreach($list_items as $list_item)
echo '<li>' . $list_item . '</li>';
echo '</ul>';
}
}
?>
回答1:
- 1- add
;
before explode function, and remove accolades. - 2- declare a second variable different than
$list_items
where to put result of explode. - 3- second parameter of get_post_meta() should be the slug of your custom field (in your case is it idid?), add also true parameter.
Your code will look like:
<?php
$list_items = get_post_meta($post->ID, 'idid', true);
if($list_items){
$list_items2 = explode(" ", $list_items);
echo '<ul>';
foreach($list_items2 as $list_item)
echo '<li>' . $list_item . '</li>';
echo '</ul>';
}
?>
来源:https://stackoverflow.com/questions/15913825/wordpress-output-custom-field-as-ul-list