.seo-meta-box { background: #fff; border: 1px solid #ccd0d4; box-shadow: 0 1px 1px rgba(0,0,0,.04); } .seo-field { margin-bottom: 15px; } .seo-field label { display: block; font-weight: 600; margin-bottom: 5px; } .seo-field input, .seo-field textarea { width: 100%; padding: 8px 12px; border: 1px solid #ddd; border-radius: 4px; } .seo-field textarea { height: 80px; resize: vertical; } .seo-field .description { font-size: 13px; color: #666; margin-top: 5px; } .seo-preview { background: #f9f9f9; padding: 15px; border-radius: 4px; margin-top: 15px; } .seo-preview h4 { margin: 0 0 10px 0; color: #333; } .seo-title-preview { color: #1a0dab; font-size: 18px; text-decoration: none; } .seo-url-preview { color: #006621; font-size: 14px; margin: 2px 0; } .seo-desc-preview { color: #545454; font-size: 13px; line-height: 1.4; } .char-count { float: right; font-size: 12px; color: #666; } .char-count.warning { color: #d63638; } '; } } /** * 后台管理页面 */ public function admin_page() { if (isset($_POST['submit'])) { // 保存设置 update_option('nenghui_seo_global_title', sanitize_text_field($_POST['global_title'])); update_option('nenghui_seo_global_description', sanitize_textarea_field($_POST['global_description'])); update_option('nenghui_seo_global_keywords', sanitize_text_field($_POST['global_keywords'])); update_option('nenghui_seo_separator', sanitize_text_field($_POST['separator'])); update_option('nenghui_seo_home_title', sanitize_text_field($_POST['home_title'])); update_option('nenghui_seo_home_description', sanitize_textarea_field($_POST['home_description'])); update_option('nenghui_seo_home_keywords', sanitize_text_field($_POST['home_keywords'])); echo '

设置已保存!

'; } // 获取当前设置 $global_title = get_option('nenghui_seo_global_title', get_bloginfo('name')); $global_description = get_option('nenghui_seo_global_description', get_bloginfo('description')); $global_keywords = get_option('nenghui_seo_global_keywords', ''); $separator = get_option('nenghui_seo_separator', '-'); $home_title = get_option('nenghui_seo_home_title', ''); $home_description = get_option('nenghui_seo_home_description', ''); $home_keywords = get_option('nenghui_seo_home_keywords', ''); ?>

SEO 全局设置

标题分隔符

用于分隔页面标题和网站名称

全局网站标题

网站的全局标题,会出现在所有页面标题的后面

全局网站描述

网站的全局描述,用于没有设置单独描述的页面

全局关键词

网站的全局关键词,用逗号分隔

首页专用设置

首页标题

首页专用标题,留空则使用全局标题

首页描述

首页专用描述,留空则使用全局描述

首页关键词

首页专用关键词,留空则使用全局关键词

ID, '_nenghui_seo_title', true); $seo_description = get_post_meta($post->ID, '_nenghui_seo_description', true); $seo_keywords = get_post_meta($post->ID, '_nenghui_seo_keywords', true); ?>
0/60
建议长度:50-60个字符,留空则使用文章标题
0/160
建议长度:120-160个字符,留空则自动提取文章摘要
多个关键词用逗号分隔,建议3-5个关键词

搜索结果预览

post_title); ?>
ID)); ?>
post_content, 20)); ?>
get_seo_title(); $description = $this->get_seo_description(); $keywords = $this->get_seo_keywords(); // 输出标题 echo '' . esc_html($title) . '' . "\n"; // 输出描述 if ($description) { echo '' . "\n"; } // 输出关键词 if ($keywords) { echo '' . "\n"; } // Open Graph 标签 echo '' . "\n"; if ($description) { echo '' . "\n"; } echo '' . "\n"; echo '' . "\n"; // Twitter Card 标签 echo '' . "\n"; echo '' . "\n"; if ($description) { echo '' . "\n"; } } /** * 获取SEO标题 */ private function get_seo_title() { $separator = get_option('nenghui_seo_separator', '-'); $global_title = get_option('nenghui_seo_global_title', get_bloginfo('name')); if (is_home() || is_front_page()) { $home_title = get_option('nenghui_seo_home_title'); return $home_title ?: $global_title; } if (is_singular()) { global $post; $custom_title = get_post_meta($post->ID, '_nenghui_seo_title', true); if ($custom_title) { return $custom_title; } return $post->post_title . ' ' . $separator . ' ' . $global_title; } if (is_category()) { $category = get_queried_object(); return $category->name . ' ' . $separator . ' ' . $global_title; } if (is_tag()) { $tag = get_queried_object(); return $tag->name . ' ' . $separator . ' ' . $global_title; } if (is_archive()) { return get_the_archive_title() . ' ' . $separator . ' ' . $global_title; } if (is_search()) { return '搜索: ' . get_search_query() . ' ' . $separator . ' ' . $global_title; } if (is_404()) { return '页面未找到 ' . $separator . ' ' . $global_title; } return $global_title; } /** * 获取SEO描述 */ private function get_seo_description() { $global_description = get_option('nenghui_seo_global_description', get_bloginfo('description')); if (is_home() || is_front_page()) { $home_description = get_option('nenghui_seo_home_description'); return $home_description ?: $global_description; } if (is_singular()) { global $post; $custom_description = get_post_meta($post->ID, '_nenghui_seo_description', true); if ($custom_description) { return $custom_description; } // 自动提取摘要 if ($post->post_excerpt) { return wp_trim_words($post->post_excerpt, 25); } return wp_trim_words(strip_tags($post->post_content), 25); } if (is_category()) { $category = get_queried_object(); return $category->description ?: $global_description; } if (is_tag()) { $tag = get_queried_object(); return $tag->description ?: $global_description; } return $global_description; } /** * 获取SEO关键词 */ private function get_seo_keywords() { $global_keywords = get_option('nenghui_seo_global_keywords'); if (is_home() || is_front_page()) { $home_keywords = get_option('nenghui_seo_home_keywords'); return $home_keywords ?: $global_keywords; } if (is_singular()) { global $post; $custom_keywords = get_post_meta($post->ID, '_nenghui_seo_keywords', true); if ($custom_keywords) { return $custom_keywords; } // 自动提取标签作为关键词 $tags = get_the_tags($post->ID); if ($tags) { $tag_names = array(); foreach ($tags as $tag) { $tag_names[] = $tag->name; } return implode(', ', $tag_names); } } return $global_keywords; } /** * 获取当前页面URL */ private function get_current_url() { global $wp; return home_url(add_query_arg(array(), $wp->request)); } } // 初始化SEO功能 new NenghuiSEO();