Created
July 10, 2011 11:57
-
-
Save miya0001/1074485 to your computer and use it in GitHub Desktop.
ページや投稿の抜粋からdescriptionを出力する
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
<?php | |
add_action('wp_head', 'add_description'); | |
function add_description() { | |
$meta = '<meta name="description" content="%s" />'; | |
if (is_home() || is_front_page()) { | |
$site_description = get_bloginfo('description', 'display'); | |
if ($site_description) { | |
printf($meta, $site_description); | |
} | |
} elseif (is_singular()) { | |
$id = get_the_ID(); | |
$post = &get_post($id); | |
$exc = null; | |
if ($post->post_excerpt) { | |
$exc = esc_attr($post->post_excerpt); | |
} else { | |
$exc = esc_attr(strip_tags($post->post_content)); | |
$exc = preg_replace("/[\r\n]/", " ", $exc); | |
$exc = mb_strimwidth($exc, 0, 100, "..."); | |
} | |
if ($exc) { | |
printf( | |
$meta, | |
$exc | |
); | |
} | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment