【问题标题】:Why do I get every title of each post when I filter them in single.php当我在 single.php 中过滤它们时,为什么我会得到每个帖子的每个标题
【发布时间】:2013-11-05 16:21:10
【问题描述】:

我只是在页面和帖子中处理过滤器标题,一切都按预期工作,但是当我查看 single.php 而它显示实际帖子时,我正在处理的函数挂钩过滤器在 functions.php 中我的插件正在过滤我博客每篇文章的每个标题,我希望只过滤当前文章的标题:

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

任何帮助表示感谢。

谢谢 ;)

【问题讨论】:

    标签: php wordpress filtering title


    【解决方案1】:
    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; 
    }
    

    当您查看任何帖子时,它会通过 single.php 显示,因此每个帖子在您查看时都会成为当前帖子

    【讨论】:

    • 我现在明白,对于过滤器标题挂钩,从 single.php 查看单个帖子与查看整个博客以进行过滤器相同。有没有一种方法可以过滤条件以仅在 single.php 中检索当前帖子的标题?我确实尝试过这样的事情,但显然我不能嵌套它。
    【解决方案2】:

    我现在明白,对于过滤器标题挂钩,查看来自 single.php 的单个帖子与查看整个博客是一样的。有没有一种过滤方法,只在 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());
          }
        }    
    }
    

    【讨论】:

      猜你喜欢
      • 2019-06-22
      • 1970-01-01
      • 1970-01-01
      • 2020-09-17
      • 1970-01-01
      • 2011-11-10
      • 2022-09-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多