简体   繁体   English

为什么在single.php中过滤每个帖子的每个标题

[英]Why do I get every title of each post when I filter them in single.php

I am just working on filter titles in pages and posts and everything works as expected but when I view the single.php whereas it shows the actual post as should be, the function hook filter i am working on inside functions.php of my plugin is filtering every title of each post of my blog, I was expecting to filter just the title of the current post: 我只是在处理页面和帖子中的过滤器标题,并且一切都按预期工作,但是当我查看single.php时,它按实际显示了实际的帖子,我正在处理插件的functions.php中的函数挂钩过滤器是过滤博客中每个帖子的每个标题,我希望只过滤当前帖子的标题:

add_filter( 'the_title', 'ta_modified_post_title');
function ta_modified_post_title ($title) { 
    if((in_the_loop())){ 
      /**/
    }    
}

any help is grateful. 任何帮助都将不胜感激。

Thanks ;) 谢谢 ;)

add_filter( 'the_title', 'ta_modified_post_title');
function ta_modified_post_title ($title) { 
    if((some condition true)){ 
     $add_this = 'some string';
     $new_title = $title.' '.$add_this;
      return $new_title;
    }   

      return $title; 
}

when you view any post it is shown through single.php so every post becomes current post when you view it 当您查看任何帖子时,它将通过single.php显示,因此当您查看它时,每个帖子都将成为当前帖子

I understand now that for the filter title hook it is the same viewing a single post from single.php than viewing the whole blog. 我现在知道,对于过滤器标题挂钩,查看single.php中的单个帖子与查看整个博客是相同的。 would there be a way of filtering with a condition to only retrieve the title of the current post in single.php?, I did try "get_the_title(get_the_ID())" like this but obviously I cant nest it. 有没有一种条件过滤的方法,只能在single.php中检索当前帖子的标题?,我确实尝试过这样的“ get_the_title(get_the_ID())”,但显然我不能嵌套它。

add_filter( 'the_title', 'ta_modified_post_title');
function ta_modified_post_title ($title) { 
    if((in_the_loop())){ 
      if(is_single()){
        $title=get_the_title(get_the_ID());
      }
    }    
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM