在你的榜樣這個
$output = '<a class="w-portfolio-item-anchor" href="javascript:void(0);" data-id="'.$post->ID.'">' . $output;
將創建此$output
:
<a class="w-portfolio-item-anchor" href="javascript:void(0);" data-id="'.$post->ID.'"><div class="w-portfolio">
<div class="w-portfolio-h">
<div class="w-portfolio-list">
<div class="w-portfolio-list-h">
提示:在代碼中使用var_dump($output)
或echo $ouput
,以便您可以查看存儲的內容在$輸出變量。就像你現在的代碼似乎你會有一個混亂的HTML結構。
編輯:
要初始化的第35行,現在我正在寫一些僞代碼應該如何工作循環的$output
變量外:
//line 34
$output = '<div class="w-portfolio">
<div class="w-portfolio-h">
<div class="w-portfolio-list">
<div class="w-portfolio-list-h">';
foreach($posts as $post) { // Note: This is the while loop in your example
$output .= '<a href="javascript:void(0);" data-id="'.$post->ID.'">'; //notice the .= which appends the string to variable declared on line 35
}
$output .= '</div>
</div>
</div>
</div>';
return $output;
不要使用$output .= $output
或$output = 'bla bla' . $ouput
這裏的任何地方。這會破壞你想用你的代碼創建的HTML結構。注意您的$輸出變量以$output = '<div class="w-portfolio">
開頭。
請刪除的. $output;
從</div>'. $output;
在你的代碼行105。
試試這個PasteOfCode
只是一個小的評論的事情;爲什麼不使用[heredocs](http://stackoverflow.com/a/5673478/899126)來保存多行字符串,尤其是因爲您仍然可以在其中嵌入變量? – 2014-10-28 18:58:33
更具相關性:你認爲它打破了JavaScript的工作方式是什麼意思?控制檯日誌中是否顯示任何內容?如果您查看頁面源代碼,那麼生成的HTML看起來是否正確? – 2014-10-28 19:01:46
@ChrisForrence呃,heredocs我以前沒有用過 - 我會研究這個,謝謝! - 無論輸出如何生成,頁面源都完全相同。這是爲什麼我不明白這是行不通的原因之一。 – Patrick 2014-10-28 19:21:18