2013-08-01 19 views
0

我创建了一个新的wordpress插件,它只在帖子中显示,但为了检测它是一个帖子,我尝试使用is_single(),但它不起作用。WordPress的单一undefined

class myplugin{ 
//my plugin code here 
} 
function load_plugin($plugin_class, $priority = 10) { 
    if (class_exists($plugin_class)) { 
     add_action("init", 
       create_function('', "global \$$plugin_class; \$$plugin_class = new $plugin_class();"), 
       $priority); 
    } 
} 
if(is_single()){ // witout this, the plugin is displayed everywhere, but whit it it's not displayed at all 
load_plugin(myplugin); 
} 

我甚至想看到的is_single

echo"<script>alert('".is_single()."');</script>"; 

输出我得到 “未定义”

编辑//不用其他的is_single,只是加载我的插件,我的插件适用于wordpress的每一页。

+0

你检查了错误日志吗? “不工作”并没有给我们很多去... – naththedeveloper

+0

错误解决;) – Rezrazi

回答

1

Conditional tags,就像is_single一样,直到wp hook已经被触发才可用。你试图使用它太早,这就是为什么它返回undefined。

然后将函数添加到钩子,然后在那里执行is_single测试。这种开销很少,所以不要担心性能问题。

+0

啊,是的,没有注意到!谢谢。 – Rezrazi

+0

很高兴有帮助。不要忘记接受答案:) – Mark

相关问题