我正在嘗試注冊 WordPress 短代碼。
我有一個包含 HTML 的 PHP 回傳陳述句。在 HTML 中,我有一個 javascript onclick 函式。Visual Studio Code 拋出語法錯誤,即意外的“框架”(T_STRING),需要“,”或“;”
我已經閱讀了關于 Stack Overflow 的其他文章,我嘗試轉義字串內的單引號,但我可能會不準確地轉義。下面是一些沒有任何轉義的原始代碼。
我知道下面的代碼可能不太好,但仍然感謝所有幫助。
<?php
function torque_hello_world_shortcode() {
return '<div ></div>
<a onclick="document.getElementById('frame').style.display = document.getElementById('frame').style.display == 'none' ? 'block' : 'none'; return false;"><img src="#" alt="WordPress 簡碼 PHP 檔案中的語法錯誤" style="width:60px;height:60px;cursor:pointer !important;">
<p style="cursor:pointer !important;">text! <span style="cursor:pointer !important;">more text</span></p></a>
<div ></div>';
}
add_shortcode( 'helloworld', 'torque_hello_world_shortcode' );
uj5u.com熱心網友回復:
您在 return 陳述句中混合了單引號和雙引號(在字串的第 2 行,您基本上關閉了字串并在它后面加上了單詞“frame”,而不使用連接甚至 $ 變數符號)。
如果用單引號打開一個字串,第二個單引號將關閉該字串。如果你需要使用單引號內的字串,你需要逃避它,用反斜杠:echo 'Arnold once said: "I\'ll be back"';
我添加反斜杠您的代碼:
<?php
function torque_hello_world_shortcode() {
return '<div ></div>
<a onclick="document.getElementById(\'frame\').style.display =
document.getElementById(\'frame\').style.display == \'none\' ? \'block\' :
\'none\'; return false;"><img src="#" alt="WordPress 簡碼 PHP 檔案中的語法錯誤"
style="width:60px;height:60px;cursor:pointer !important;">
<p style="cursor:pointer !important;">text! <span
style="cursor:pointer !important;">more text</span></p></a>
<iframe id="frame" src="#" allowfullscreen="" frameborder="0">
</iframe>
<div ></div>';
}
add_shortcode( 'helloworld', 'torque_hello_world_shortcode' );
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/324555.html
標籤:php WordPress的 短代码
