亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

PHP 在一個變量中包含另一個變量中的 HTML

PHP 在一個變量中包含另一個變量中的 HTML

PHP
狐的傳說 2021-07-06 13:49:46
我想在另一個變量中的 HTML 中添加一個保存函數(無論是 WordPress 還是自定義函數)的變量。問題是,當我連接它時,它會彈出前端的 div 容器之外,我需要它在容器內。例如此處顯示的,我希望在這些 div 中生成“$stay_put”或 PHP:function sort_sections( $sections ) {      $sections = explode(',', $sections);      $output = '';      /* $stay put needs to be able to hold any function */      $stay_put = wp_list_pages();      if ( empty( $sections ) ) {          return $output;      }      foreach( $sections as $section ) {        switch ( $section ) {        case 'section_a':            $output .= '<div>Section A</div>';            break;        case 'section_b':            $output .= '<div>Section B</div>';            break;        default:            break;        }      }      return $output;  }我想出了但在容器外顯示變量:$stay_putforeach( $sections as $section ) {  switch ( $section ) {  case 'section_a':      $output .= '<div>' . $stay_put . '</div>';      break;  case 'section_b':      $output .= '<div>' . $stay_put . '</div>';      break;  default:      break;  }}如果有人可以幫忙,先感謝您。
查看完整描述

1 回答

?
呼啦一陣風

TA貢獻1802條經驗 獲得超6個贊

您的示例代碼的主要問題是您想要返回輸出,但對 的調用wp_list_pages沒有返回所需的信息,而是直接回顯它。如果要將 的結果添加wp_list_pages到輸出中,則必須將參數添加到wp_list_pages. 根據wordpress 文檔,您必須設置echo為false.


要在每個部分的div后面添加,請看下面的代碼:


function render_sections( $sections ) {

      $sections = explode(',', $sections);

      $output = '';

      $stay_put = wp_list_pages(['echo' => false);

      if ( empty( $sections ) ) {

          return $output;

      }

      foreach( $sections as $section ) {

        switch ( $section ) {

        case 'section_a':

            $output .= "<div>Section A</div>';

            $output .= $stay_put;

            break;

        case 'section_b':

            $output .= '<div>Section B</div>';

            $output .= $stay_put;

            break;

        default:

            break;

        }

      }

      return $output;

  }

請注意,我已將函數名稱從 更改為sort_sections,render_sections因為這似乎更接近于描述其功能(干凈的代碼)。


查看完整回答
反對 回復 2021-07-09
  • 1 回答
  • 0 關注
  • 251 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號