admin管理员组文章数量:1130349
What i mean is that.
My categories are:
Music. Video Etc...
So i want post title per category to be like this.
Post in music = should be "Download Music: wp_title()"
Post in video = should be "Download Video: wp_title()
What i mean is that.
My categories are:
Music. Video Etc...
So i want post title per category to be like this.
Post in music = should be "Download Music: wp_title()"
Post in video = should be "Download Video: wp_title()
1 Answer
Reset to default 0You have already mentioned the function wp_title(). Right before outputting the title, it passes its value through the filter wp_title, which can be used here to prepend with additional information.
add_filter('wp_title', 'WPSE_20181106_prepend_title', 10, 3);
function WPSE_20181106_prepend_title($title, $sep, $seplocation) {
// not a single post
if (!is_singular('post')) {
return $title;
}
// IDs of categories that should prepend the title
$prepend_categories = [15, 35];
// get all categories of post
$categories = get_the_category();
foreach ($categories as $category) {
// found category
if (in_array($category->term_id, $prepend_categories)) {
// return new format, using __() so it is translateable
return sprintf('%s %s: %s',
__('Download', 'lang-slug'),
$category->name,
$title
);
}
}
// category not found, return default
return $title;
}
What i mean is that.
My categories are:
Music. Video Etc...
So i want post title per category to be like this.
Post in music = should be "Download Music: wp_title()"
Post in video = should be "Download Video: wp_title()
What i mean is that.
My categories are:
Music. Video Etc...
So i want post title per category to be like this.
Post in music = should be "Download Music: wp_title()"
Post in video = should be "Download Video: wp_title()
1 Answer
Reset to default 0You have already mentioned the function wp_title(). Right before outputting the title, it passes its value through the filter wp_title, which can be used here to prepend with additional information.
add_filter('wp_title', 'WPSE_20181106_prepend_title', 10, 3);
function WPSE_20181106_prepend_title($title, $sep, $seplocation) {
// not a single post
if (!is_singular('post')) {
return $title;
}
// IDs of categories that should prepend the title
$prepend_categories = [15, 35];
// get all categories of post
$categories = get_the_category();
foreach ($categories as $category) {
// found category
if (in_array($category->term_id, $prepend_categories)) {
// return new format, using __() so it is translateable
return sprintf('%s %s: %s',
__('Download', 'lang-slug'),
$category->name,
$title
);
}
}
// category not found, return default
return $title;
}
本文标签: customizationPlease I want to prefix my WP posts title according to each category
版权声明:本文标题:customization - Please I want to prefix my WP posts title according to each category 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749202669a2332055.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论