Skip to content

Instantly share code, notes, and snippets.

@matesnippets
Last active August 29, 2015 14:17
Show Gist options
  • Select an option

  • Save matesnippets/6493ab289435c07b255f to your computer and use it in GitHub Desktop.

Select an option

Save matesnippets/6493ab289435c07b255f to your computer and use it in GitHub Desktop.
PHP, WordPress, make nice content to description tag
/**
* Put some nice content into a description meta tag
*
* Usage:
* <meta name="description" content="<?php echo ud_site_desc(); ?>">
*
* @return string 200 characters long descrption tag
*/
function ud_site_desc()
{
global $post;
if (is_single() || is_page()) {
// On a single post and pages get first 200 characters
// from the content
$args = array(
'p' => $post->ID,
'post_type' => get_post_type()
);
$custom_posts = get_posts($args)[0]->post_content;
$custom_posts = strip_shortcodes($custom_posts);
$custom_posts = strip_tags($custom_posts);
$custom_posts = trim($custom_posts);
$custom_posts = str_truncater($custom_posts, 200);
return $custom_posts;
} else {
// Otherwise get the site description
return get_bloginfo('description');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment