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 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题