开发者

Update the wp title using function or code wihout updating header.php file

I got a requirement to update the blog page title with the recent post title. I got the result using this code and its working. I am using get_header() and page title is in header file. I have different templates in theme and blog is also a template page because I am using wp as CMS.

One simple options is copy paste the header code into blog template and appl开发者_Python百科y the title. Is there any other possibility to modify the title using functions or code without copy pasting complete header code into template file.

$query = new WP_Query( array ( 'orderby' => 'date', 'order' => 'DESC', 'post_type'=>'post') );
$queried_post = get_post($query->post->ID);
$title = $queried_post->post_title;
_e($title);


Yes, you can hook into the wp_title callback (filter) and change it then. You then do not need to modify each theme's template file. Your callback function then needs to return the new title:

function my_title($currentTitle) { # ignoring other settings for now
  $query = new WP_Query( array ( 'orderby' => 'date', 'order' => 'DESC', 'post_type'=>'post') );
  $queried_post = get_post($query->post->ID);
  return $queried_post->post_title;
}

add_filter('wp_title', 'my_title');
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜