2014-02-20 4 views
6

이 코드를 사용하여 WordPress 페이지에 항목을 추가하고 있습니다. 그러나이 코드는 작동하지 않습니다. is_page()은 항상 false 값과 함께 반환됩니다.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... 
     } 
    } 
} 

도와주세요.

+0

당신이 is_page ('페이지 이름')를 시도해야을; ? – Zeeshan

+0

예, 사용되었습니다. 하지만 작동하지 않는 ... : ( –

답변

18

이 좋아 .. 여기에 솔루션입니다 확인할 수 있습니다 같아요. action hook init 대신 wp action hook을 사용하십시오.

/* 
* 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'; 
     } 
    } 
} 

잘 작동합니다. 희망이 도움이됩니다.

+0

3 시간 후에 시험을 저장했습니다.이 동작은 WordPress 웹 사이트의 어딘가에서 설명해야합니다. – phpheini

0

그럼 나는 그것이 작동하지 않는 경우가 $wpdb 객체를 움켜 잡고 post_type

function only_pages(){ 
    global $wpdb; 
    if(!is_admin() & $wpdb->post_type == 'page'): 
     //Do something here 
    endif; 
} 
+0

$ wpdb-> post_type null로 반환했습니다. –

-1
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. 

Reference

-1

대체 방법을 시도해보십시오. 모든 페이지에는 body css에 "page"라는 클래스가 있습니다. 그래서 당신은이 클래스를 타겟팅 할 수 있습니다 :

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