我对php很陌生。
我正在使用高级自定义字段,并且有一个名为tags
. 用户正在输入以下内容:
Tag 1, Tag 2, Tag 3
我想分析此输出并将其显示为无序列表:标签1,标签2,标签3,下面的代码给出了字段。
<div><?php the_field(\'tags\'); ?></div>
有什么建议吗,如何将输入解析为无序列表html?非常感谢您的回复!
我对php很陌生。
我正在使用高级自定义字段,并且有一个名为tags
. 用户正在输入以下内容:
Tag 1, Tag 2, Tag 3
我想分析此输出并将其显示为无序列表:标签1,标签2,标签3,下面的代码给出了字段。
<div><?php the_field(\'tags\'); ?></div>
有什么建议吗,如何将输入解析为无序列表html?非常感谢您的回复!
这更多的是关于php的字符串和数组操作,而不是WordPress相关的问题。
正确的方法是使用转发器字段,而不是单一的文本输入。高级自定义字段似乎就是您正在使用的插件,它有一个很好的repeater插件。这将允许用户添加任意多的字段、更改顺序、删除字段等等。无需担心正确写入分隔符。
如果不选择使用repeater字段,则可以使用php函数explode
将字符串转换为数组,然后使用简单循环进行处理。
//The custom field
$custom_field = \'Tag 1, Tag 2, Tag 3\';
//Remove the extra space after the comma
$custom_field = str_replace( \', \', \',\', $custom_field );
//Convert the string to array using the comma as the delimiter
$items = explode( \',\' , $custom_field );
//Print the unordered list
echo \'<ul>\';
foreach ( $items as $item ) :
echo "<li>$item</li>";
endforeach;
echo \'</ul>\';
我想更改我网站的登录URLhttp://example.com/wp-admin 到http://example.com/example.我不想使用插件来实现这一点,所以我可以从函数中实现这一点。php?