最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

functions - only update titles of single posts

programmeradmin1浏览0评论

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
Add a comment  | 

1 Answer 1

Reset to default 0

If 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);
发布评论

评论列表(0)

  1. 暂无评论