簡體   English   中英

ECHO 語句中的 PHP 函數

[英]PHP Function inside of ECHO statement

我在 Wordpress 中使用下面的這個函數:

function wpstudio_doctype() {
  $content = '<!DOCTYPE html>' . "\n";
  $content .= '<html ' . language_attributes() . '>';
    echo apply_filters( 'wpstudio_doctype', $content );
}

問題是該函數在<!DOCTYPE html>標簽上方顯示$content ,而不是在HTML標簽內添加字符串。

我在這里做錯了什么?

language_attributes()不返回屬性,它會回應它們。

// last line of language_attributes()
echo apply_filters( 'language_attributes', $output );

這意味着它將在您的字符串組裝之前顯示。 您需要使用輸出緩沖捕獲此值,然后將其附加到您的字符串中。

// Not sure if the output buffering conflicts with anything else in WordPress
function wpstudio_doctype() {
  ob_start();
  language_attributes();
  $language_attributes = ob_get_clean();

  $content = '<!DOCTYPE html>' . "\n";
  $content .= '<html ' . $language_attributes . '>';
    echo apply_filters( 'wpstudio_doctype', $content );
}

而不是輸出緩沖,只需在 ECHO 語句中使用get_language_attributes() 在這種特定情況下:

function wpstudio_doctype() {
  $content = '<!DOCTYPE html>' . "\n";
  $content .= '<html ' . get_language_attributes() . '>';
    echo apply_filters( 'wpstudio_doctype', $content );
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM