add_action ('the_title' , 'test');
function test($title) {
$mycode = '<div>test</div>';
echo $mycode . $title;
}
I expected this functions.php code to add "test" before the post title. It does that. But it adds it also all over the place on the page. Not sure what's the logic, it seems it adds appends it to all menu items of the page.
Am I wrong on my usage of the_title? Or might there be theme interference here?
the_title
is used for all page titles, post titles, and menu item labels (menu items are technically posts). If you only want it to apply to certain titles, you need to specify which in your code (just posts? just the current page? etc.). Also,the_title
is a filter, not an action. So you should useadd_filter()
andreturn
the result, not echo it.