高级自定义字段:显示为项目符号



我在Wordpress中创建了一个自定义复选框字段,用户可以在列出的属性中选择各种可用的设施。

我正在使用元素构建一个页面模板。

当我从ACF导入数据时,它显示为逗号分隔的列表。

是否有任何方法可以让这个显示为项目符号列表?

下面是一段代码,用于从ACF获得项目列表形式的输出数据-

<?php
$arr = get_the_field('field_name');
$str = explode (",", $arr);
echo '<ul>';
foreach($str as $s){
echo '<li>'.$s.'</li>';
}
echo '</ul>';
?>

我希望这是你正在寻找的。

你可以尝试使用一个插件,让你创建php代码片段,并运行他们的短代码,如https://it.wordpress.org/plugins/insert-php/

一旦你创建了php代码片段,你就可以尝试使用Elementor的短代码小部件来运行它。

我会稍微调整一下Tahmid的精彩回答。为了允许空行(没有项目符号),在您的functions.php文件中使用此命令:

/**
* create a ACF text field with bullets
* Empty lines stay empty
* @param string $field_name Name of the field to bullet
*/
function acf_bullets(string $field_name): void {
$format_acf_bullet = function(
string $value,
int $post_id,
array $form ):string {
if( empty($value) ) {
return '';
}
$lines = explode( "n", $value );
$result = "<ul class="theme-ul">n";
foreach( $lines as $line) {
if( strlen($line)<=1 ) { // empty line, start a new list
$result .= "</ul><p/><ul class="theme-ul">n";
} else {
$result .= "<li>".$line."n";
}
}
$result .= "</ul>n";
return $result;
};
add_filter("acf/format_value/name=$field_name", $format_acf_bullet, 10, 3);
}

acf_bullets('your-fieldname-here');

表示

最新更新