admin管理员组

文章数量:1391929

The below function wraps all titles of all pages in a * symbol. I want to change the function so that it only applies the filter to titles of single posts. How can I accomplish this?

function apply_titles($title, $id)  {
     return "* ".$title." *";
     return $title;
}

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

The below function wraps all titles of all pages in a * symbol. I want to change the function so that it only applies the filter to titles of single posts. How can I accomplish this?

function apply_titles($title, $id)  {
     return "* ".$title." *";
     return $title;
}

add_filter('the_title', 'apply_titles', 10, 2);
Share Improve this question asked Mar 16, 2020 at 7:57 A BanitabaA Banitaba 31 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

If you want to wrap post title of posts only, then you should use get_post_type() to check post type of post. You can take reference from below code, it will applied on "post" post type only. and want to apply on post single page then you have to add is_single() with get_post_type() condition.

function apply_titles($title, $id)  {
     if('post' == get_post_type($id) && is_single())
     {
        return "* ".$title." *";
     }
     return $title;
}
add_filter('the_title', 'apply_titles', 10, 2);

本文标签: functionsonly update titles of single posts