admin管理员组

文章数量:1122832

I would like to add an html element right before the title of the blog posts. The code I'm using is as follows:

function add_html_above_title($title, $post_id) {
    if (is_single() && $post_id === get_the_ID()) {
        $html = '<div class="custom-html">Custom HTML content above the title</div>';
        // Return the HTML element followed by the original title
        return $html . $title;
    }
    return $title;
}

add_filter('the_title', 'add_html_above_title', 10, 2);

However, the problem with this code is that it also alters the titles used in other parts of the blog post template, like the title of the post used in the breadcrumb. How can I target only the main post title and not the other ones? Can't I for example first check if the title I'm going to modify is inside a specific css class and only then modify it?

Update: The output of debug_backtrace() ran on functions.php in the child theme:

I would like to add an html element right before the title of the blog posts. The code I'm using is as follows:

function add_html_above_title($title, $post_id) {
    if (is_single() && $post_id === get_the_ID()) {
        $html = '<div class="custom-html">Custom HTML content above the title</div>';
        // Return the HTML element followed by the original title
        return $html . $title;
    }
    return $title;
}

add_filter('the_title', 'add_html_above_title', 10, 2);

However, the problem with this code is that it also alters the titles used in other parts of the blog post template, like the title of the post used in the breadcrumb. How can I target only the main post title and not the other ones? Can't I for example first check if the title I'm going to modify is inside a specific css class and only then modify it?

Update: The output of debug_backtrace() ran on functions.php in the child theme:

Share Improve this question edited May 14, 2024 at 6:06 s.sufi asked May 13, 2024 at 12:48 s.sufis.sufi 32 bronze badges 4
  • Where are you inputting this code? If you're doing it in a child theme or a custom theme, then you should have the ability to just edit the actual output template and not have to worry about fiddling with a filter. – Tony Djukic Commented May 13, 2024 at 17:49
  • @TonyDjukic I'm doing this inside the functions.php file of the child theme. How can I edit the output template? I can't figure out which template is powering the single post page as the main theme is too big and files are scattered all over the place. – s.sufi Commented May 14, 2024 at 5:13
  • 1 @TonyDjukic I found the template and successfully edited it. Thanks. – s.sufi Commented May 14, 2024 at 8:08
  • Fantastic. :-) Glad I was able to help. – Tony Djukic Commented May 14, 2024 at 22:48
Add a comment  | 

1 Answer 1

Reset to default 1

There are several ways to achieve this, none of them elegant. That's because there is no general way to know exactly where get_the_title was called.

(1) Not completely foolproof, but possibly enough in your scenario would be to add is_main_query to your conditions. That would rule out titles generated in secondary loops. However, if the breadcrumb is generated inside the loop, this would obviously not work.

(2) You could use debug_backtrace to find which template file called get_the_title and add this being single.php as a condition. Assuming the theme uses such a template. Also assuming there is no secondary loop in this template.

(3) You could skip the filter on the server side and convert your code to a piece of javascript that detects the presence of a certain class and insert the html on the user end.

本文标签: singleHow to edit specific title instead of all the title calls