我看到错误 "Call to undefined function pods()",即使 pods() 已定义
I am seeing error "Call to undefined function pods()" even though pods() is defined
在 WordPress 内部,我有两个插件。
第一个插件名为Pods,它有一个pods()
功能。
第二个插件(我创建的)是 Pods 的简单插件,它使用 Pods()
函数,如下所示:
<?php
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
//Get the pod for the current post where this shortcode will be appearing
$pod = pods( get_post_type(), get_the_ID() );
//Build the name shortcode
add_shortcode( 'my_name', 'bg_my_name_shortcode' );
function bg_my_name_shortcode($pod) {
$my_name = $pod->display('my_name');
return $my_name;
}
但由于某种原因,这会导致错误 Uncaught Error: Call to undefined function pods()
,即使 pods()
是在另一个 Pods 插件中定义的,并且它被设计为像这样扩展:https://pods.io/docs/code/pods/
如果我在 bg_my_name_shortcode
函数中移动 $pod = pods( get_post_type(), get_the_ID() );
它工作正常,但我有很多这样的简码要制作所以我不想调用这三个函数(pods()
, get_post_type()
, get_the_ID()
) 一遍又一遍,而不是调用一次并将其存储为变量。
我也很困惑为什么会发生这种情况,因为 pods()
绝对是 Pods 插件中定义的函数。
您收到该错误的原因是定义该函数的插件尚未加载。
您需要在WordPress初始化并加载所有插件后声明短代码。
试试这个代码:
<?php
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
function bg_my_name_shortcode_init(){
//Get the pod for the current post where this shortcode will be appearing
$pod = pods( get_post_type(), get_the_ID() );
//Build the name shortcode
add_shortcode( 'my_name', 'bg_my_name_shortcode' );
function bg_my_name_shortcode($pod) {
$my_name = $pod->display('my_name');
return $my_name;
}
}
add_action('init', 'bg_my_name_shortcode_init');
可以找到更多详细信息here
编辑
修复 Uncaught Error: Call to a member function display() on string
错误:
<?php
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
function bg_my_name_shortcode_init(){
function bg_my_name_shortcode() {
//Get the pod for the current post where this shortcode will be appearing
$pod = pods( get_post_type(), get_the_ID() );
$my_name = $pod->display('my_name');
return $my_name;
}
//Build the name shortcode
add_shortcode( 'my_name', 'bg_my_name_shortcode' );
}
add_action('init', 'bg_my_name_shortcode_init');
在 WordPress 内部,我有两个插件。
第一个插件名为Pods,它有一个pods()
功能。
第二个插件(我创建的)是 Pods 的简单插件,它使用 Pods()
函数,如下所示:
<?php
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
//Get the pod for the current post where this shortcode will be appearing
$pod = pods( get_post_type(), get_the_ID() );
//Build the name shortcode
add_shortcode( 'my_name', 'bg_my_name_shortcode' );
function bg_my_name_shortcode($pod) {
$my_name = $pod->display('my_name');
return $my_name;
}
但由于某种原因,这会导致错误 Uncaught Error: Call to undefined function pods()
,即使 pods()
是在另一个 Pods 插件中定义的,并且它被设计为像这样扩展:https://pods.io/docs/code/pods/
如果我在 bg_my_name_shortcode
函数中移动 $pod = pods( get_post_type(), get_the_ID() );
它工作正常,但我有很多这样的简码要制作所以我不想调用这三个函数(pods()
, get_post_type()
, get_the_ID()
) 一遍又一遍,而不是调用一次并将其存储为变量。
我也很困惑为什么会发生这种情况,因为 pods()
绝对是 Pods 插件中定义的函数。
您收到该错误的原因是定义该函数的插件尚未加载。
您需要在WordPress初始化并加载所有插件后声明短代码。 试试这个代码:
<?php
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
function bg_my_name_shortcode_init(){
//Get the pod for the current post where this shortcode will be appearing
$pod = pods( get_post_type(), get_the_ID() );
//Build the name shortcode
add_shortcode( 'my_name', 'bg_my_name_shortcode' );
function bg_my_name_shortcode($pod) {
$my_name = $pod->display('my_name');
return $my_name;
}
}
add_action('init', 'bg_my_name_shortcode_init');
可以找到更多详细信息here
编辑
修复 Uncaught Error: Call to a member function display() on string
错误:
<?php
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
function bg_my_name_shortcode_init(){
function bg_my_name_shortcode() {
//Get the pod for the current post where this shortcode will be appearing
$pod = pods( get_post_type(), get_the_ID() );
$my_name = $pod->display('my_name');
return $my_name;
}
//Build the name shortcode
add_shortcode( 'my_name', 'bg_my_name_shortcode' );
}
add_action('init', 'bg_my_name_shortcode_init');