高级自定义字段:显示为子弹点

发布于 2025-02-11 13:18:10 字数 142 浏览 15 评论 0原文

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

我正在使用Elementor构建页面模板。

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

有什么办法可以将其显示为项目符号列表?

I have a custom checkbox field created in Wordpress where the user can select various facilities available at a listed property.

I am building a page template using Elementor.

When I import the data from ACF, it displays as a comma separated list.

Is there any way I can instead get this to display as as a bulleted list?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浮生面具三千个 2025-02-18 13:18:10

这是一个代码,可以从ACF作为项目符号列表获取输出数据 -

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

我希望这就是您想要的。

Here is a code to get the output data from ACF as a bulleted list -

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

I hope this is what you are looking for.

无声无音无过去 2025-02-18 13:18:10

您可以尝试使用一个插件,该插件可以创建PHP代码段,并使用诸如此类的快捷代码运行: https://it.wordpress.org/plugins/insert-php/

创建了PHP片段后,您可以尝试使用Elementor的快捷代码窗口窗口使用快速代码来运行它。

You could try using a plugin that let's you create php code snippets and run them with a shortcode such as this: https://it.wordpress.org/plugins/insert-php/

Once you created the php snippet you could try to run it with a shortcode using Elementor's shortcode widget.

脸赞 2025-02-18 13:18:10

我会稍微调整Tahmid的出色答案。为了允许空行(无子弹)在you 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."</li>\n";
            }
        }
        $result .= "</ul>\n";
        return $result;
    };
    add_filter("acf/format_value/name=$field_name", $format_acf_bullet, 10, 3);
}

使用acf_bullets调用此('your-fieldname-here'here');

dixplisation

  • 为特定字段添加过滤器调用$ format_acf_bulltets
  • 跳过完整的空截面
  • 行创建一个阵列,
  • 如果
  • 为所有
  • 行为空启动新&lt; ul&gt;列表
  • 如果不包装&lt; li&gt;&lt;/li&gt;
  • 关闭&lt; ul&gt;

完整列表内容应用于字符串中变量$ result并在最后返回。练习使班级名称theme-ul acf_bullets的参数

I'd slightly adjust Tahmid's excellent answer. In order to allow for empty lines (without a bullet) use this in you functions.php file:

/**
 * 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."</li>\n";
            }
        }
        $result .= "</ul>\n";
        return $result;
    };
    add_filter("acf/format_value/name=$field_name", $format_acf_bullet, 10, 3);
}

Call this with acf_bullets('your-fieldname-here');

Explanation

  • Add a filter calling $format_acf_bulltets for the specific field that:
  • Skips complete empty sections
  • create an array for all the lines
  • open a new list
  • for all entries
  • if the line is empty start a new <ul> list
  • if not wrap the line in <li></li>
  • close the <ul>

The complete list content is appended in string variable $result and returned at the end. As exercise make the class name theme-ul a parameter for acf_bullets

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文