programing

워드프레스 is_page()는 항상 false와 함께 반환됩니다.

minimums 2023. 3. 29. 21:21
반응형

워드프레스 is_page()는 항상 false와 함께 반환됩니다.

워드프레스 페이지에 내용을 추가하는 데 이 코드를 사용하고 있습니다.하지만 이 코드는 작동하지 않습니다.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...
        }
    }
}

제발 도와주세요.

좋아요, 여기 해결책이 있어요액션 후크 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';
        }
    }
}

이거 잘 돼.이게 도움이 됐으면 좋겠다.

그게 안되면, 내 생각엔 네가 이걸 집어들어$wpdb오브젝트 및 체크post_type

function only_pages(){
     global $wpdb;
     if(!is_admin() & $wpdb->post_type == 'page'):
         //Do something here
     endif;
}
is_page();
// When any single Page is being displayed.

is_page( 42 );
// When Page 42 (ID) is being displayed.

is_page( 'Contact' );
// When the Page with a post_title of "Contact" is being displayed.

is_page( 'about-me' );
// When the Page with a post_name (slug) of "about-me" is being displayed.

is_page( array( 42, 'about-me', 'Contact' ) );
// Returns true when the Pages displayed is either post ID 42, or post_name "about-me", or post_title "Contact".  Note: the array ability was added at Version 2.5.

언급

다른 방법을 시도해 보세요.모든 페이지의 본문 css에는 "page"라는 이름의 클래스가 있습니다.따라서 다음 클래스를 대상으로 할 수 있습니다.

    $classes = get_body_class();
    if (in_array('page',$classes)) {
        // Do something
    }

언급URL : https://stackoverflow.com/questions/21898891/wordpress-is-page-always-returned-with-false

반응형