Skip to content

Instantly share code, notes, and snippets.

@miya0001
Created July 10, 2011 11:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miya0001/1074485 to your computer and use it in GitHub Desktop.
Save miya0001/1074485 to your computer and use it in GitHub Desktop.
ページや投稿の抜粋からdescriptionを出力する
<?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