Last active
September 20, 2015 10:38
-
-
Save koki-h/14f2413a12a8808479dc to your computer and use it in GitHub Desktop.
wordpressのbizvectorテーマでdescriptionの内容をカスタマイズする例。metadescriptionCustomというフィルターフックを使う。
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
| <?php | |
| //Descritptionにカテゴリ名・カスタムフィールドの内容を入れる | |
| add_filter('metadescriptionCustom', 'customize_description'); | |
| //変数 $metadesctiption には元々のdescriptionが入ってくる | |
| function customize_description($metadescription) { | |
| global $post; | |
| //今回は特定の投稿タイプの個別ページのみ | |
| if (get_post_type($post) != "sitelist" || !is_single() ) { | |
| return $metadescription; //何もしない | |
| } | |
| $description = "コメント件数:" . $post->comment_count . "件" . " | " ; | |
| $categories = wp_get_object_terms($post->ID, 'genre'); | |
| if ( !empty($categories) && !is_wp_error( $categories ) ) { | |
| $description = $description . "カテゴリ:" . $categories[0]->name . " | "; | |
| $items = array("フィールド1", "フィールド2", "フィールド3"); | |
| foreach($items as $k) { | |
| if(get_post_meta($post->ID, $k, true)) { | |
| $description = $description . $k.":". get_post_meta($post->ID, $k, true) . " | "; | |
| } | |
| } | |
| } | |
| //カスタムフィールドの内容と元々のDescritptionを繋げる | |
| $description = $description . $metadescription; | |
| //不要な文字を削除するなど | |
| $description = strip_shortcodes( $description ); | |
| $description = str_replace( ']]>', ']]>', $description ); | |
| $description = strip_tags( $description ); | |
| $description = str_replace( "\xc2\xa0", " ", $description ); | |
| $description = trim( preg_replace( '/[\n\r\t ]+/', ' ', $description), ' ' ); | |
| $description = mb_convert_kana($description,'sKV','UTF-8'); | |
| $description = mb_substr( $description, 0, 120, 'UTF-8' ); | |
| $_find_pos = mb_strrpos($description,'。',0,'UTF-8'); | |
| if (($_find_pos >= 1) && ($_find_pos <= 120 - 1)) { | |
| $description = mb_substr( $description, 0, $_find_pos + 1, 'UTF-8' ); | |
| } | |
| return $description; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment