我目前使用的是php 7.2,当我在单个模板之外的函数中使用get\\u the\\u content()或get\\u the\\u extract()时。例如,我得到以下警告:
警告:count():参数必须是在/Applications/MAMP/htdocs/wordpress/wp kona/wp includes/post模板中实现可数的数组或对象。php在线284
怎么了?这是wordpress的核心bug吗?我错过了什么吗。
我目前使用的是php 7.2,当我在单个模板之外的函数中使用get\\u the\\u content()或get\\u the\\u extract()时。例如,我得到以下警告:
警告:count():参数必须是在/Applications/MAMP/htdocs/wordpress/wp kona/wp includes/post模板中实现可数的数组或对象。php在线284
怎么了?这是wordpress的核心bug吗?我错过了什么吗。
是的,我已经知道你所经历的原因:
get_the_content
或get_the_excerpt
将返回该错误:count(pages)要解决这个问题,可以很容易地看出您正在打电话get_the_content
或get_the_excerpt
循环外部。
因此,在使用之前必须正确设置post数据,这就是循环的目的:
while ( have_post(): the_post() ) : // get_the_content() or get_the_excerpt() will work fine now the_content(); endwhile;
自v5起。2,这将不再是一个问题。get_the_content()
和wp_trim_excerpt()
正在获取的可选新参数(分别为第三个和第二个)$post
因此,您可以在循环之外使用它,使其与所有其他get_the_*
功能。只花了16年时间。。。Reference on Trac
此错误与WordPress核心错误相关https://core.trac.wordpress.org/ticket/42814
通常在调用函数时发生get_the_excerpt
或get_the_content
如果摘录或内容为空,则在循环外部。
所以,要自己修复它,您应该找到错误的地方,并手动检查现有的摘录/内容。
例如:
if(has_excerpt($post_id)) {
$meta[] = sprintf(\'<meta name="description" content="%s">\',
get_the_excerpt($post_id)
);
}
如果有人偶然发现这个问题,解决方案是从元字段中获取\\u内容,并通过\\u内容过滤器运行它,如下所示:
apply_filters(\'the_content\', get_post_field(\'post_content\', $post->id));
我是WordPress的新手,我开发了一个代码,可以在小部件中单击带有某些样式的按钮后加载iframe。但是它破坏了模板的布局,我想知道如何将我的函数添加到模板中。<a id=\"fazer_pedido\" href=\"https://example.com\" onclick=\"document.getElementById(\'load-chat\'); this.style.cursor = \'default\'; this.style.opacity = \'0.1\