1

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!

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;
}
  • It looks like you have self answered. Good going. Leave some time to see if anyone else has anything to contribute and then accept an answer (yours is a valid choice) so people know that there is an acceptable answer. – Matthew Brown aka Lord Matt Oct 18 at 2:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.