Wordpress-为什么可以';我不能在插件中使用条件标签吗?
我正在尝试让以下代码正常工作:Wordpress-为什么可以';我不能在插件中使用条件标签吗?,wordpress,wordpress-theming,Wordpress,Wordpress Theming,我正在尝试让以下代码正常工作: if( is_home() ): echo 'User is on the homepage.'; else: echo 'User is not on the homepage'; endif; 如果我把它放在主题的页眉或页脚中,它就可以工作,但是如果我把它放在我的插件中,它就不工作了。我也尝试过is_single()和is_page()。知道问题出在哪里吗?is_home()和其他一些WP函数并不总是定义的,请尝试使用合适的钩子来包含代
if( is_home() ):
echo 'User is on the homepage.';
else:
echo 'User is not on the homepage';
endif;
如果我把它放在主题的页眉或页脚中,它就可以工作,但是如果我把它放在我的插件中,它就不工作了。我也尝试过is_single()
和is_page()。知道问题出在哪里吗?is_home()
和其他一些WP函数并不总是定义的,请尝试使用合适的钩子来包含代码。例如:
add_action('wp', 'check_home');
// or add_action('init', 'check_home');
function check_home($param)
{
if (is_home()):
echo 'User is on the homepage.';
else:
echo 'User is not on the homepage';
endif;
}
编辑:
在任何情况下,如果要回显数据,请使用主体
标记内的挂钩。使用内容钩子的示例:
add_filter('the_content', 'check_home');
function check_home($content)
{
if (is_home())
$echo = 'User is on the homepage.';
else
$echo = 'User is not on the homepage';
return $echo . '<hr />' . $content;
}
add_filter('the_content','check_home');
功能检查\u主页($content)
{
if(is_home())
$echo='用户在主页上';
其他的
$echo='用户不在主页上';
返回$echo.“
”。$content;
}