Wordpress - output custom field as ul list

2019-08-31 06:07发布

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楼-- · 2019-08-31 06:55
  • 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>';

        }

    ?>
查看更多
登录 后发表回答