public
Last active

Excerpt as meta description.

  • Download Gist
excerpt-meta-desc.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
<?php
add_action( 'wp_head', 'cor_meta_desc_wp_head' );
/**
* Excerpt as meta description.
*
* Echos `the_excerpt` only if one is set.
*
* @since 0.1.0
*/
function cor_meta_desc_wp_head() {
if ( ( is_home() or is_front_page() ) ) {
$options = get_option( 'default_theme_options' );
if ( ! empty( $options['home_description'] ) )
printf( '<meta content="%s" name="description" />' . "\n", $options['home_description'] );
 
} elseif ( is_post_type_archive( $post_types = 'person' ) ) {
$options = get_option( 'default_theme_options' );
if ( ! empty( $options['person_description'] ) )
printf( '<meta content="%s" name="description" />' . "\n", $options['person_description'] );
 
} elseif ( is_category() ) {
$category_description = category_description();
if ( ! empty( $category_description ) )
printf( '<meta content="%s" name="description" />' . "\n", $category_description );
 
} elseif ( is_tax() ) {
$term_description = term_description();
if ( ! empty( $term_description ) )
printf( '<meta content="%s" name="description" />' . "\n", $term_description );
 
} elseif ( is_singular() ) {
$singular_description = get_the_excerpt();
if ( ! empty( $singular_description ) )
printf( '<meta content="%s" name="description" />' . "\n", $singular_description );
}
}
substr-excerpt-meta-desc-auto.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
<?php
add_action( 'wp_head', 'cor_meta_desc_wp_head' );
/**
* Excerpt as meta description.
*
* Returns the 156 first characters of the string.
*
* @since 0.1.0
*/
function cor_meta_desc_wp_head() {
if ( ( is_home() or is_front_page() ) ) {
$options = get_option( 'default_theme_options' );
$meta_description = substr( $options['meta_description'], 0, 156 );
if ( ! empty( $meta_description ) )
echo '<meta content="' . $meta_description . '" name="description" />' . "\n";
 
} elseif ( is_post_type_archive( $post_types = 'person' ) ) {
$options = get_option( 'default_theme_options' );
$person_description = substr( wp_filter_nohtml_kses( $options['person_description'] ), 0, 156 );
if ( ! empty( $person_description ) )
echo '<meta content="' . $person_description . '" name="description" />' . "\n";
 
} elseif ( is_category() ) {
$category_description = substr( category_description(), 0, 156 );
if ( ! empty( $category_description ) )
echo '<meta content="' . $category_description . '" name="description" />' . "\n";
 
} elseif ( is_tax() ) {
$term_description = substr( term_description(), 0, 156 );
if ( ! empty( $term_description ) )
echo '<meta content="' . $term_description . '" name="description" />' . "\n";
 
} elseif ( is_singular() ) {
while ( have_posts() ) : the_post();
$get_the_excerpt = substr( get_the_excerpt(), 0, 156 );
echo '<meta content="' . $get_the_excerpt . '" name="description" />' . "\n";
endwhile;
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.