gpt4 book ai didi

PHP 没有在正确的地方回显

转载 作者:太空宇宙 更新时间:2023-11-04 15:08:50 24 4
gpt4 key购买 nike

我在我的 WordPress 网站上运行一些 PHP 代码,当 PHP 运行以回显某些信息时,它没有在正确的位置回显。代码的缩写版本是:

<div class="description cms">
<div id="home_left_middle">
<h2>Search By Category</h2>
[xyz-ips snippet="Category-List"]
</div>
</div>

短代码用于插件 XYZ PHP 代码,它基本上只是一种在 WordPress 帖子中使用“包含”的方法。我已经在 WP 数据库中设置了一些自定义,但这只是基本的数据库调用。所有信息都从数据库成功返回,因此没有问题。我在那里使用的 PHP 代码是:

$sql = "SELECT * FROM wp_categories_table";
$result = $wpdb->get_results($sql);

echo "<ul class='category_list'>";

foreach( $result as $results ) {
$category = $results->category;
$number = $results->number_of;
$category_html = htmlentities($category);

echo "<li><a href='?search-class=DB_CustomSearch_Widget-db_customsearch_widget&widget_number=preset-default&cs-all-0=&cs-post_cat-1=".$category_html."&search=Search'>".$category." (".$number.")</a></li>";
}

echo "</ul>";

发生的情况是,当 PHP 代码运行时,它会在 div.description 之后(在 div#home_left_middle 之前)立即回显它,并且只有 H2 保留在 home_left_middle div 中。那不是运行代码的地方。

奇怪的是,我在另一个站点上使用了完全相同的代码(我复制了这个站点,因为我们只是在更换服务器,但内容完全相同)并且它在另一个服务器上运行良好。这是一个 VPS,所以我想知道是否有某种我没有正确安装在服务器上的 PHP 扩展可能会导致这种情况?我知道这有点牵强,但我很困惑为什么在相同版本的 WordPress 中使用完全相同的代码并使用相同的主题和 CSS 文件会在两个不同的站点上导致不同的结果。我看到的唯一区别可能是服务器,除非我忽略了一些小错误?

编辑

对于那些询问输出缓冲问题的人,我最近从上一个服务器复制了 php.ini 文件,输出缓冲代码如下所示:

; output_buffering
; Default Value: Off
; Development Value: 4096
; Production Value: 4096

所以它似乎被注释掉了。

最佳答案

就像评论中建议的 maioran84,这可能是输出缓冲区的一些问题。我建议您将 php.ini 文件从工作站点复制到非工作站点服务器。

查看 PHP 配置文件,有一些设置可能会导致输出在 wordpress 准备好之前显示。

; Implicit flush tells PHP to tell the output layer to flush itself
; automatically after every output block. This is equivalent to calling the
; PHP function flush() after each and every call to print() or echo() and each
; and every HTML block. Turning this option on has serious performance
; implications and is generally recommended for debugging purposes only.
; http://php.net/implicit-flush
; Note: This directive is hardcoded to On for the CLI SAPI
implicit_flush = Off

output_buffering = 4096

查看 add_shortcode() 的 WordPress 文档,以下文本 block 确实很突出。您可以轻松更改所有 echo 语句以将值附加到字符串,然后将整个字符串返回。

Note that the function called by the shortcode should never produce output of any kind. Shortcode functions should return the text that is to be used to replace the shortcode. Producing the output directly will lead to unexpected results. This is similar to the way filter functions should behave, in that they should not produce expected side effects from the call, since you cannot control when and where they are called from.

https://codex.wordpress.org/Function_Reference/add_shortcode

希望这能帮助您解决这个问题,但我以前从未听说过发生过这样的错误。

关于PHP 没有在正确的地方回显,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22467912/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com