Wordpress is_page()始终返回false

8 php wordpress

我正在使用此代码向wordpress页面添加一些内容.但是这段代码不起作用.is_page()始终返回false值.

<?php
/**
 * Plugin Name: Name Of The Plugin
 * Plugin URI: http://URI_Of_Page_Describing_Plugin_and_Updates
 * Description: A brief description of the Plugin.
 * Version: The Plugin's Version Number, e.g.: 1.0
 * Author: Name Of The Plugin Author
 * Author URI: http://URI_Of_The_Plugin_Author
 * License: A "Slug" license name e.g. GPL2
 */
add_action("init", "only_pages");

function only_pages(){
    if(!is_admin()){
        if(is_page()){
            // Do something here...
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

请帮我.

小智 23

好的..这是解决方案.而不是使用动作钩子init,使用wp动作钩子.

    /*
 * Plugin Name: Page Test
 */

add_action("wp", "only_pages");

function only_pages(){
    if(!is_admin()){
        wp_reset_query();
        if(is_page()){
            echo 'I am single page';
        }
        else{
            echo 'I am not single page';
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这很好用.希望这可以帮助.