Using the Yoast Meta Description rather than the excerpt

written by: Jeff McNear

I recently was asked to configure an archive array to show the Yoast meta-descriptions (if populated) rather than the excerpt for an array of posts – here is how I did it:

   <!-- showing the Yoast meta conditionally - if no description then show excerpt (https://wordpress.stackexchange.com/questions/56597/if-get-post-meta-is-empty-do-something) -->

<?php $meta = get_post_meta( get_the_ID(), '_yoast_wpseo_metadesc', true );
    if ($meta == '') { ?>
     <?php the_excerpt(); ?>
   <?php } else {
        echo  $meta ;
      }
?>

first we set up what we are looking for:

<?php $meta = get_post_meta( get_the_ID(), '_yoast_wpseo_metadesc', true );

then we verify that it isn’t empty – and if so we default to the excerpt

    if ($meta == '') { ?>
     <?php the_excerpt(); ?>

If it is populated, then we go ahead and use it

   <?php } else {
        echo  $meta ;
      }
?>