the_title
filter has two parameters passed to it, $title
and $id
. You could use the $id
to check the current post type and then do stuff based on that.
add_action ('the_title' , 'test', 10, 2);
function test($title, $id) {
return 'post' === get_post_type($id) ? '<div>test</div> ' . $title : $title;
}
The problem with this is that the filter is kind of not aware in which context it is called (naturally WP conditionals provide some context). So you might get unexpected results and end up with the custom title prefix/suffix showing up everywhere on a single view, e.g. the main menu, main title, related posts, sidebar, footer…, even if you just wanted to make the modification in only one part of your view. This of course depends on your setup, needs and use case.