diff --git a/header.php b/header.php index 8deec2e..53f323d 100644 --- a/header.php +++ b/header.php @@ -3,7 +3,7 @@ * 主题页眉 * @author Seaton Jiang * @license MIT License - * @version 2020.04.12 + * @version 2020.06.13 */ ?> @@ -14,8 +14,8 @@ <?php wp_title( '-', true, 'right' ); ?> - - + + diff --git a/inc/theme-article.php b/inc/theme-article.php index 8d2485e..56b0000 100644 --- a/inc/theme-article.php +++ b/inc/theme-article.php @@ -382,4 +382,60 @@ function recover_comment_fields($comment_fields){ $comment_fields = array_merge($comment_fields ,array('comment' => $comment)); return $comment_fields; } -add_filter('comment_form_fields','recover_comment_fields'); \ No newline at end of file +add_filter('comment_form_fields','recover_comment_fields'); + +$new_meta_boxes = +array( + "description" => array( + "name" => "seo_description", + "std" => "", + "title" => __( '描述', 'kratos' ) +), + "keywords" => array( + "name" => "seo_keywords", + "std" => "", + "title" => __( '关键词', 'kratos' ) +) +); + +function seo_meta_boxes() { + $post_types = get_post_types(); + add_meta_box( 'meta-box-id', __( 'SEO 设置', 'kratos' ), 'post_seo_callback', $post_types ); +} +add_action( 'add_meta_boxes', 'seo_meta_boxes' ); + +function post_seo_callback( $post ) { + global $new_meta_boxes; + + foreach($new_meta_boxes as $meta_box) { + $meta_box_value = get_post_meta($post->ID, $meta_box['name'].'_value', true); + + if($meta_box_value == "") + $meta_box_value = $meta_box['std']; + + echo '

'.$meta_box['title'].'

'; + echo '
'; + } + + echo ''; +} + +function wpdocs_save_meta_box( $post_id ) { + global $new_meta_boxes; + + if ( !wp_verify_nonce( $_POST['metaboxes_nonce'], plugin_basename(__FILE__) )) + return; + + if ( !current_user_can( 'edit_posts', $post_id )) + return; + + foreach($new_meta_boxes as $meta_box) { + $data = $_POST[$meta_box['name'].'_value']; + + if($data == "") + delete_post_meta($post_id, $meta_box['name'].'_value', get_post_meta($post_id, $meta_box['name'].'_value', true)); + else + update_post_meta($post_id, $meta_box['name'].'_value', $data); + } +} +add_action( 'save_post', 'wpdocs_save_meta_box' ); \ No newline at end of file diff --git a/inc/theme-setting.php b/inc/theme-setting.php index 23686d8..d7200da 100644 --- a/inc/theme-setting.php +++ b/inc/theme-setting.php @@ -3,7 +3,7 @@ * 站点相关函数 * @author Seaton Jiang * @license MIT License - * @version 2020.04.12 + * @version 2020.06.13 */ // 标题配置 @@ -28,61 +28,54 @@ add_filter('wp_title', 'title', 10, 2); // Keywords 配置 function keywords() { - if (is_home() || is_front_page()) { - echo kratos_option('seo_keywords'); - } elseif (is_category()) { - echo kratos_option('seo_keywords') . ','; - single_cat_title(); + global $post; + if (is_home()) { + $keywords = kratos_option('seo_keywords'); } elseif (is_single()) { - echo trim(wp_title('', false)) . ','; - echo kratos_option('seo_keywords') . ','; - if (has_tag()) { - foreach (get_the_tags() as $tag) { - echo $tag->name . ','; + $keywords = get_post_meta($post->ID, "seo_keywords_value", true); + if($keywords == '') { + $tags = wp_get_post_tags($post->ID); + foreach ($tags as $tag ) { + $keywords = $keywords . $tag->name . ", "; } + $keywords = rtrim($keywords, ', '); } - foreach (get_the_category() as $category) { - echo $category->cat_name . ','; + } elseif (is_page()) { + $keywords = get_post_meta($post->ID, "seo_keywords_value", true); + if($keywords == '') { + $keywords = kratos_option('seo_keywords'); } - } elseif (is_search()) { - echo kratos_option('seo_keywords') . ','; - the_search_query(); } else { - echo kratos_option('seo_keywords') . ','; - echo trim(wp_title('', false)); + $keywords = single_tag_title('', false); } + return trim(strip_tags($keywords)); } // Description 配置 function description() { - if (is_home() || is_front_page()) { - echo trim(kratos_option('seo_description')); - } elseif (is_category()) { - $description = strip_tags(category_description()); - echo trim($description); + global $post; + if (is_home()) { + $description = kratos_option('seo_description'); } elseif (is_single()) { - if (get_the_excerpt()) { - echo get_the_excerpt(); - } else { - global $post; - $description = trim(str_replace(array("\r\n", "\r", "\n", " ", " "), " ", str_replace("\"", "'", strip_tags($post->post_content)))); - echo mb_substr($description, 0, 220, 'utf-8'); + $description = get_post_meta($post->ID, "seo_description_value", true); + if ($description == '') { + $description = get_the_excerpt(); } - } elseif (is_search()) { - echo '「'; - the_search_query(); - echo '」共找到 '; - global $wp_query; - echo $wp_query->found_posts; - echo ' 个记录'; + if ($description == '') { + $description = str_replace("\n","",mb_strimwidth(strip_tags($post->post_content), 0, 200, "…", 'utf-8')); + } + } elseif (is_category()) { + $description = category_description(); } elseif (is_tag()) { - $description = strip_tags(tag_description()); - echo trim($description); - } else { - $description = strip_tags(term_description()); - echo trim($description); + $description = tag_description(); + } elseif (is_page()) { + $description = get_post_meta($post->ID, "seo_description_value", true); + if ($description == '') { + $description = kratos_option('seo_description'); + } } + return trim(strip_tags($description)); } // robots.txt 配置 diff --git a/languages/kratos.pot b/languages/kratos.pot index 775ff93..ff85da2 100644 --- a/languages/kratos.pot +++ b/languages/kratos.pot @@ -3,7 +3,7 @@ msgid "" msgstr "" "Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\n" "Project-Id-Version: Kratos\n" -"POT-Creation-Date: 2020-06-08 14:23+0800\n" +"POT-Creation-Date: 2020-06-13 13:30+0800\n" "PO-Revision-Date: 2020-02-14 23:32+0800\n" "Last-Translator: \n" "Language-Team: \n" @@ -233,6 +233,18 @@ msgstr "" msgid "添加表情" msgstr "" +#: inc/theme-article.php:392 +msgid "描述" +msgstr "" + +#: inc/theme-article.php:397 inc/theme-options.php:218 +msgid "关键词" +msgstr "" + +#: inc/theme-article.php:403 +msgid "SEO 设置" +msgstr "" + #: inc/theme-core.php:67 msgid "您已经赞过了" msgstr "" @@ -441,10 +453,6 @@ msgstr "" msgid "搜索引擎或者社交工具分享首页时抓取的图片" msgstr "" -#: inc/theme-options.php:218 -msgid "关键词" -msgstr "" - #: inc/theme-options.php:219 msgid "每个关键词之间需要用「英文逗号」分割" msgstr ""