【发布时间】:2019-03-23 14:07:47
【问题描述】:
我正在编写一个插件,您可以在其中在页面中输入一个短代码,它会显示一个数据表。
但是,短代码之前的页面内容被随机插入到表格的中间。当我刷新页面时,在简码上方键入的内容会在简码生成的表格中随机移动。
短代码下的内容不会出现在短代码返回中。
有谁知道为什么会发生这种情况。这太奇怪了。
------------------ wordpress 页面编辑---------------
这里有一些内容。
这是另一段。
[view_contributions]
页面内容结束。
--------------------wordpress 页面编辑结束------------------------ ----
然后产生
-----显示--------
[短代码数据表带有“这是一些内容。这是另一个段落。”随机插入某处的单元格中。然后更多数据表]
页面内容结束。
-------- 显示结束 -----
这太奇怪了。就好像简码首先呈现,然后 WordPress 将页面内容注入到简码正在呈现的任何内容中。有什么想法可能导致这种情况吗?
编辑:添加完整的代码以防发生真正奇怪的事情......
function soco_view_contributions_shortcode() {
$view_contributions = Soco_Contributions::soco_display_contributions();
return $view_contributions;
}
add_shortcode( 'view_contributions', 'soco_view_contributions_shortcode');
public function soco_display_contributions() {
$contribution_results = Soco_Contributions::soco_get_contributions_view();
ob_start;
?>
<div name="div-output-container">
<form name="frm-search-contributions">
<table width="100%" border="0">
<tbody>
<tr>
<th scope="col">Start Date</th>
<th scope="col">End Date</th>
<th scope="col">Minimum</th>
<th scope="col">Maximum</th>
<th scope="col">Name</th>
<th scope="col">Event</th>
</tr>
<tr>
<td><input type="date" name="start-date"></td>
<td><input name="end-date" type="date" ></td>
<td><input type="number" name="low-number"></td>
<td><input type="number" name="high-number"></td>
<td><text name="txt-auto-name"> </textarea></td>
<td><select> </select></td>
</tr>
</tbody>
</table>
<input type="submit">
<input type="reset">
</form>
<table width="100%" border="0">
<tbody>
<tr>
<th scope="col">Date</th>
<th scope="col">Amount</th>
<th scope="col">Cycle</th>
<th scope="col">Name</th>
<th scope="col">Event</th>
</tr>
<?php foreach ($contribution_results as $cr) { ?>
<tr>
<td><?php echo $cr->contribution_date ?></td>
<td><?php echo $cr->amount ?></td>
<td><?php echo $cr->cycle_amount ?></td>
<td><?php echo $cr->last_name.', '.$cr->first_name ?></td>
<td> </td>
</tr>
<?php } ?>
</tbody>
</table>
<button name="btnDownload" id="btnDownload" title="Click this button to download the above dataset." >Download CSV File</button>
</div>
<?php
$contribution_output = ob_get_clean();
return $contribution_output;
}
【问题讨论】:
-
你在哪里刷新输出缓冲?
-
我需要在某处使用 ob_flush 或 flush 吗?我看到的例子只是在开头使用了ob_start。然后 ob_get_clean 最后没有其他任何东西。我过去曾使用过它,并且效果很好。它现在不是那样工作的。如果我需要在代码中做其他事情,请告诉我。感谢您查看此内容!
-
输出缓冲可能是这种情况下的因素。我将添加一个可能对您的案例有所帮助的答案。
标签: php wordpress render shortcode