我有一個 wordpress 實例,我想通過 php 函數設置內聯 html 標簽的背景圖像。html是:<div id="content" class="site-content" <?php echo get_background_for_single(get_the_ID(), is_single()); ?>>上面的 html 和 php 在瀏覽器中有以下輸出:<div id="content" class="site-content" style="background-image: url(" http:="" localhost="" wiese="" wp-content="" uploads="" 2019="" 06="" test.jpg");="">在functions.php我得到:function get_background_for_single($ID, $single) { if ($single == 1) { return 'style="background-image: url("' . get_the_post_thumbnail_url($ID) . '");'; }}如果我按如下方式控制段落中的輸出,則顯示正確:<p><?php echo get_background_for_single(get_the_ID(), is_single()); ?><p>它以 ap 標簽返回:style="background-image: url("http://localhost/wiese/wp-content/uploads/2019/06/test.jpg");為什么函數返回“=”而不是“/”?也許這是一個轉儲錯誤,但我看不到我的錯誤。
1 回答

DIEA
TA貢獻1820條經驗 獲得超2個贊
將我的功能更改為以下是解決方案:
function get_background_for_single($ID, $single) {
if ($single == 1) {
return 'style="background-image: url("' . get_the_post_thumbnail_url($ID) . '");"';
}
}
- 1 回答
- 0 關注
- 200 瀏覽
添加回答
舉報
0/150
提交
取消