Wordpress-将自定义字段输出为ul列表
问题描述:
我有一个自定义字段,其内容我想作为ul列表输出。
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-添加
;
爆炸功能之前,并删除赞誉。 - 2-声明第二个变量,该变量不同于
$ list_items
爆炸结果放在何处。 - 3- get_post_meta()的第二个参数应该是自定义字段的子项(在您的情况下,它是idid ?),还添加true参数。
- 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.
您的代码应如下所示:
<?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>';
}
?>