customization - Dynamic title in Wordpress homepage

admin2025-04-19  0

My homepage setting is "Display the latest post's" and I have the number set to 1. So my homepage show's my latest post.

I have YOAST plugin setting the title in homepage for now. I would like to customize the home page's title as below...

  • If yoast is setting the title as "Today's breaking news", and if my latest post has title "Earthquake in LA"...
  • I want the homepage's title tag to have text as "Today's breaking news - Earthquake in LA".
  • This means, the title will be dynamic and change with each new post.

I would prefer to edit code and achieve this instead of a plugin. I did a bit of searching and I did not get much help. Here is what I have now... I tried putting this into the child theme's functions.php and it did not change anything...

add_filter('wpseo_title', 'filter_keral_wpseo_title');
function filter_keral_wpseo_title($title) {
    if(  is_front_page() ) {
        $recent = get_posts(array(
        'author'=>1,
        'orderby'=>'date',
        'order'=>'desc',
        'numberposts'=>1
        ));
        if( $recent ){
        $title = get_the_title($recent[0]->ID);
        }
    }
    return $title;
}

Thanks for your time!

My homepage setting is "Display the latest post's" and I have the number set to 1. So my homepage show's my latest post.

I have YOAST plugin setting the title in homepage for now. I would like to customize the home page's title as below...

  • If yoast is setting the title as "Today's breaking news", and if my latest post has title "Earthquake in LA"...
  • I want the homepage's title tag to have text as "Today's breaking news - Earthquake in LA".
  • This means, the title will be dynamic and change with each new post.

I would prefer to edit code and achieve this instead of a plugin. I did a bit of searching and I did not get much help. Here is what I have now... I tried putting this into the child theme's functions.php and it did not change anything...

add_filter('wpseo_title', 'filter_keral_wpseo_title');
function filter_keral_wpseo_title($title) {
    if(  is_front_page() ) {
        $recent = get_posts(array(
        'author'=>1,
        'orderby'=>'date',
        'order'=>'desc',
        'numberposts'=>1
        ));
        if( $recent ){
        $title = get_the_title($recent[0]->ID);
        }
    }
    return $title;
}

Thanks for your time!

Share Improve this question edited Oct 17, 2019 at 14:37 Thomas Koipuram asked Oct 17, 2019 at 14:11 Thomas KoipuramThomas Koipuram 214 bronze badges 1
  • Welcome to WordPress Development. I hope you find the answer(s) you are looking for. Our site is different from most - if you have not done so yet, consider checking out the tour and help center to find out how things work. – Matthew Brown aka Lord Matt Commented Oct 18, 2019 at 2:54
Add a comment  | 

1 Answer 1

Reset to default 1

Actually the above code (reposted below) worked perfectly. Guess it was a cache issue that it did not get reflected. Thanks for your time!

add_filter('wpseo_title', 'filter_keral_wpseo_title');
function filter_keral_wpseo_title($title) {
    if(  is_front_page() ) {
        $recent = get_posts(array(
        'author'=>1,
        'orderby'=>'date',
        'order'=>'desc',
        'numberposts'=>1
        ));
        if( $recent ){
        $title = get_the_title($recent[0]->ID);
        }
    }
    return $title;
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745071998a283338.html

最新回复(0)