Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ページや投稿の抜粋から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
You can’t perform that action at this time.