dougu4448 2015-09-21 04:17
浏览 45
已采纳

每行上的WordPress元框输出

Ive been looking for a way I can take a WordPress custom meta box's content that is entered on each line and output said contents on each line instead of in a single line, example:

Mon
Tues
Wed
Thurs
Fri
Sat
Sun

The above days of the week are entered in a custom meta box and I can output the contents of the meta box in my single.php with:

<?php if ( 'custom_post_type' == get_post_type() ) :
    $foobar_meta = get_post_meta( get_the_ID(), 'meta-textarea', true );
    if( !empty( $foobar_meta ) ) : ?>
        <div class="jedi">
            <ul id="workload">
                <li><?php echo $foobar_meta; ?></li>
            </ul>
        </div>
    <?php endif; ?>
<?php endif; ?>

However when I run the code it outputs as: <li>Mon Tues Wed Thurs Fri Sat Sun</li>. How can I target everything that is entered on a line and output individually as:

<li>Mon</li>
<li>Tues</li>
<li>Wed</li>
<li>Thurs</li>
<li>Fri</li>
<li>Sat</li>
<li>Sun</li>

Per my research I assume I would use an array() with a foreach but targeting each line I am having difficulties in what to use to call the meta box to run through. I did look on the codex but I didn't see anything on how to target what I need. So how can I with PHP take content that is entered on a single line and display it's look on the site?

  • 写回答

1条回答 默认 最新

  • dongmeng2687 2015-09-21 04:52
    关注

    Please see below code this may help you:

    $string_arr = explode("
    ", $foobar_meta);
    $newstring = '<ul id="workload">';
    foreach($string_arr as $single_string)
    {
        $newstring .= '<li>' . $single_string . '</li>';
    }
    $newstring .= '</ul>';
    echo $newstring;
    

    OR

    You can use like this too $newstring .= '<li>' . str_replace(" ", '</li><li>', $single_string) . '</li>'; without loop and explode.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 彩灯控制电路,会的加我QQ1482956179
  • ¥200 相机拍直接转存到电脑上 立拍立穿无线局域网传
  • ¥15 (关键词-电路设计)
  • ¥15 如何解决MIPS计算是否溢出
  • ¥15 vue中我代理了iframe,iframe却走的是路由,没有显示该显示的网站,这个该如何处理
  • ¥15 操作系统相关算法中while();的含义
  • ¥15 CNVcaller安装后无法找到文件
  • ¥15 visual studio2022中文乱码无法解决
  • ¥15 关于华为5g模块mh5000-31接线问题
  • ¥15 keil L6007U报错