Skip to main content
added 178 characters in body
Source Link

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:

enter image description here

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?

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:

enter image description here

formatting and readability
Source Link
cjbj
  • 15k
  • 16
  • 42
  • 89

I would like to add an htmlhtml 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 for example 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 csscss class and only then modify it?

Thanks.

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 for example 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?

Thanks.

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?

Source Link

How to edit specific title instead of all the title calls

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 for example 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?

Thanks.