class_exists不工作WordPress

时间:2014-07-19 08:47:38

标签: php wordpress class

我在WordPress functions.php中有以下函数:

if ( ! function_exists( 'mm_single_slider' ) ) :
function mm_single_slider($echo = true){


if ( class_exists( 'RW_Meta_Box' ) ) { 

    //Do Something

} else {

    //Do other things

}

}
add_action('plugins_loaded','mm_single_slider' );

但是class_exists不起作用,返回false。这个类在一个激活的插件中,显然存在。

我还在我的functions.php文件中:

require_once ('inc/mm-metabox.php');

此文件包含类似函数,其中class_exists正在工作,返回true。

我尝试使用'plugins_loaded'钩子而没有它。

我做错了什么?似乎当需要文件时,class_exists正在工作,但不是直接在functions.php文件中?

非常感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

   function mm_single_slider($echo = true){
       if ( function_exists( 'mm_single_slider' ) ) 
       return;

       if ( class_exists( 'RW_Meta_Box' ) ) { 

//Do Something

       } else {

//Do other things

       }

在你的函数文件中,你的函数之外不应该有任何代码。你的if语法也有错误。您正在使用的方法的正确方法是

 if(cond): 
   do something;
 else:
   something else;
 endif;

有一个结尾;在20/14的那个功能结束时。

您的问题将取决于您放置上述代码的位置。如果它在插件中,当您调用插件代码运行时的代码时,mm_single_slider可能不存在。因此,您将函数存在于函数中并挂钩以便稍后运行。

如果你把它放在你的主题中,插件会在主题之前运行,所以你加载插件的主题中挂钩的任何东西都不会在钩子通过时被调用。请参阅http://codex.wordpress.org/Plugin_API/Action_Reference了解加载顺序。