Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wordpress-为什么可以';我不能在插件中使用条件标签吗?_Wordpress_Wordpress Theming - Fatal编程技术网

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; }