Last active
August 29, 2015 14:17
-
-
Save matesnippets/6493ab289435c07b255f to your computer and use it in GitHub Desktop.
PHP, WordPress, make nice content to description tag
This file contains hidden or 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
| /** | |
| * 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