-
-
Save gorobey/fe68f0a519083ac6d25941f785428aa9 to your computer and use it in GitHub Desktop.
Add Meta Description to WordPress Theme
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function custom_get_excerpt($post_id) { | |
$temp = $post; | |
$post = get_post( $post_id ); | |
setup_postdata( $post ); | |
$excerpt = esc_attr(strip_tags(get_the_excerpt())); | |
wp_reset_postdata(); | |
$post = $temp; | |
return $excerpt; | |
} | |
function custom_add_meta_description_tag() { | |
?> | |
<meta name="description" content="<?php if ( is_single() || is_page() ) { | |
$excerpt = custom_get_excerpt(get_the_ID()); | |
echo $excerpt; | |
} else { | |
bloginfo('description'); | |
} | |
?>" /> | |
<?php | |
} | |
add_action('wp_head', 'custom_add_meta_description_tag', 1); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment