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
1 Answer
Reset to default 0If 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);