add_filter('document_title_parts', 'test', 10, 1);
function test($title) {
if (is_single()) {
$categories = get_the_category();
if (!empty($categories)) {
$title['title'] .= ' — ' . $categories[0]->name;
}
} elseif (is_category()) {
$title['title'] = single_cat_title('', false);
}
return $title;
}