I'm wanting to get my excerpts outside the loop and I learned how for everything but the blog page. After researching this topic I was able to produce two Q&As:
- Get excerpt using get_the_excerpt outside a loop
- Using wp_trim_excerpt to get the_excerpt() outside the loop
Since these two Q&As were several years old I wanted to make sure there wasn't any modification or additions to the approach so with further research I was able to run across:
After reading all sources I was able to produce a conditional:
<?php if ( is_front_page() ) : ?>
<meta name="twitter:description" property="og:description" itemprop="description" content="<?php echo wp_kses_post( wp_trim_words( $post->post_content, 30 ) ); ?>" />
<?php elseif ( is_home() ) : ?>
<meta property="og:description" content="<?php echo wp_kses_post( wp_trim_words( $post->post_excerpt, 20 ) ); ?>" />
<?php elseif ( is_single() || is_page() ) :
if ( empty( $post->post_excerpt ) ) : ?>
<meta property="og:description" content="<?php echo wp_kses_post( wp_trim_words( $post->post_content, 20 ) ); ?>" />
<?php else : ?>
<meta property="og:description" content="<?php echo wp_kses_post( $post->post_excerpt ); ?>" />
<?php endif; ?>
<?php endif; ?>
However, when I call is_home()
it generates a blank excerpt: <meta property="og:description" content="" />
. Why am I not able to get the excerpt for the blog page?