Wordpress - output custom field as ul list

自古美人都是妖i 提交于 2019-12-25 04:55:24

问题


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:


  • 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>';

        }

    ?>


来源:https://stackoverflow.com/questions/15913825/wordpress-output-custom-field-as-ul-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!