wordpress不同页面调用不同keywords和description

发布时间:2026/7/22 7:21:54
wordpress不同页面调用不同keywords和description 在WordPress的header.php文件中可以通过判断当前页面的类型(如首页、文章页、分类页等)然后动态地为每个页面设置不同的keywords和description。以下是一个示例代码展示如何实现这一功能!DOCTYPE html html ?php language_attributes(); ? head meta charset?php bloginfo(charset); ? meta nameviewport contentwidthdevice-width, initial-scale1 title?php wp_title(|, true, right); ?/title link relprofile hrefhttps://gmpg.org/xfn/11 link relstylesheet href?php bloginfo(stylesheet_url); ? ?php // 获取当前页面的类型 if (is_home()) { // 首页 $keywords 首页关键词1, 首页关键词2, 首页关键词3; $description 这是网站首页的描述内容; } elseif (is_single()) { // 文章页 $keywords get_post_meta(get_the_ID(), keywords, true); // 假设文章自定义字段中有关键词 $description get_post_meta(get_the_ID(), description, true); // 假设文章自定义字段中有描述 if (!$keywords) { $keywords 文章关键词1, 文章关键词2, 文章关键词3; // 默认值 } if (!$description) { $description 这是文章的默认描述内容; // 默认值 } } elseif (is_category()) { // 分类页 $cat_id get_query_var(cat); $cat get_category($cat_id); $keywords $cat-name . , 分类关键词1, 分类关键词2; $description 这是分类页面的描述内容; } elseif (is_page()) { // 页面 $keywords 页面关键词1, 页面关键词2, 页面关键词3; $description 这是页面的描述内容; } else { // 其他页面 $keywords 通用关键词1, 通用关键词2; $description 这是通用页面的描述内容; } ? meta namekeywords content?php echo esc_attr($keywords); ? meta namedescription content?php echo esc_attr($description); ? ?php wp_head(); ? /head body ?php body_class(); ?说明is_home()判断是否是首页。is_single()判断是否是文章页。这里通过文章的自定义字段(keywords和description)获取内容如果没有则使用默认值。is_category()判断是否是分类页。通过分类的名称和一些默认关键词生成keywords。is_page()判断是否是页面(如“关于我们”、“联系我们”等)。wp_head()WordPress的钩子函数用于加载插件和主题的头部代码。注意事项如果你希望更灵活地管理keywords和description可以使用WordPress插件(如 Yoast SEO)这些插件提供了更强大的功能来管理每个页面的SEO元数据。但是这些插件带来的弊端也非常多最好不要使用这些插件。如果你需要为文章或页面添加自定义字段(如keywords和description)可以在文章或页面编辑页面中使用“自定义字段”功能。通过这种方式你可以为WordPress站点的不同页面动态设置keywords和description从而优化SEO。原文https://www.bamuyao.com/7117.html