2017-06-02 54 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

相关问题