2017-06-02 52 views
1

我爲WordPress創建了一個單獨的插件(通常稱爲特定於站點的插件),我添加了一個函數來顯示上次修改日期和時間。那麼,它運作良好,但我不想顯示相同的頁面,但只爲郵政。如何防止 - 上次修改日期/時間 - 不顯示在頁面上,但只顯示帖子

我應該在這段代碼中修改什麼?

function wpb_last_updated_date($content) { 
$u_time = get_the_time('U'); 
$u_modified_time = get_the_modified_time('U'); 
if ($u_modified_time >= $u_time + 86400) { 
$updated_date = get_the_modified_time('F jS, Y'); 
$updated_time = get_the_modified_time('h:i a'); 
$custom_content .= '<p class="last-updated"><b>Last updated on</b> '. $updated_date . ' at '. $updated_time .'</p>'; 
} 

    $custom_content .= $content; 
    return $custom_content; 
} 
add_filter('the_content', 'wpb_last_updated_date'); 

回答

1

您可以檢查哪些頁面正在顯示這些條件:

is_page()  //For pages 
is_single() //for posts 
is_singular() //for posts AND pages 
is_category() //for categories 
is_tag()  //for tags 
is_404()  //for 404 page 

嘗試把下面的代碼具有條件添加自定義內容,只有職位:

function wpb_last_updated_date($content) 
{ 
     $u_time = get_the_time('U'); 
     $u_modified_time = get_the_modified_time('U'); 

     if ($u_modified_time >= $u_time + 86400) 
     { 
      $updated_date = get_the_modified_time('F jS, Y'); 
      $updated_time = get_the_modified_time('h:i a'); 
      if(is_single()) 
      { 
       $custom_content .= '<p class="last-updated"><b>Last updated on</b> '. $updated_date . ' at '. $updated_time .'</p>'; 
      } 
     } 
     $custom_content .= $content; 
     return $custom_content; 
} 
add_filter('the_content', 'wpb_last_updated_date'); 

對於更完整的模板標籤清單檢查訪問: http://codex.wordpress.org/Function_Reference/is_page

+0

它運作了Ankita!萬分感謝! – ankush

0

嗨is_page()函數可以用來檢查一個頁面是帖子還是頁面,所以我們可以使用這個條件。

function wpb_last_updated_date($content) 
{ 
     $u_time = get_the_time('U'); 
     $u_modified_time = get_the_modified_time('U'); 

     if ($u_modified_time >= $u_time + 86400) 
     { 
      $updated_date = get_the_modified_time('F jS, Y'); 
      $updated_time = get_the_modified_time('h:i a'); 
      if(!is_page()) 
      { 
       $custom_content .= '<p class="last-updated"><b>Last updated on</b> '. $updated_date . ' at '. $updated_time .'</p>'; 
      } 
     } 
     $custom_content .= $content; 
     return $custom_content; 
} 
add_filter('the_content', 'wpb_last_updated_date'); 

或者您可以檢查當前的帖子類型並執行此操作,下面的代碼將僅對帖子類型「發佈」進行過濾。

function wpb_last_updated_date($content) 
{ 
     $u_time = get_the_time('U'); 
     $u_modified_time = get_the_modified_time('U'); 

     if ($u_modified_time >= $u_time + 86400) 
     { 
      $updated_date = get_the_modified_time('F jS, Y'); 
      $updated_time = get_the_modified_time('h:i a'); 
global $post; 
     if ($post->post_type == 'post') 
      { 
       $custom_content .= '<p class="last-updated"><b>Last updated on</b> '. $updated_date . ' at '. $updated_time .'</p>'; 
      } 
     } 
     $custom_content .= $content; 
     return $custom_content; 
} 
add_filter('the_content', 'wpb_last_updated_date'); 
+0

幫助感謝!感謝:D – ankush

相關問題