文章目录 插件主要功能 插件安装步骤 代码 附加说明 插件主要功能 分别自动给worpdress网站的首页,分类页,文章页,标签页,搜索结果页添加结构化数据信息,让Google等搜索引擎更佳容易了解到网站的内容和便于后续的抓取、收录、自然排名。 插件安装步骤 创建插件文件夹:在 wp-content/plugins/ 目录下创建一个新的文件夹,比如 structured-data-plugin。 添加插件文件:将上面的代码保存为 structured-data-plugin.php 文件,并放入刚创建的文件夹中。 激活插件:登录到WordPress后台,导航到“插件”页面,找到“Structured Data Plugin”并激活它。 在wordpress–>外观–>自定义–>站点身份–>站点图标–>添加图片,指定网站图标。(插件代码会自动调用网站图标) 代码 structured-data-plugin.php 文件内容: <?php /* Plugin Name: Structured Data Plugin Description: Automatically adds structured data to WordPress pages. Version: 1.5 Author: Your Name */ // 防止直接访问 if (!defined('ABSPATH')) { exit; } // 插件初始化 function sdp_init() { // 在页面头部添加结构化数据 add_action('wp_head', 'sdp_add_structured_data'); } add_action('init', 'sdp_init'); // 添加结构化数据 function sdp_add_structured_data() { $structured_data = []; $site_url = home_url(); $site_name = get_bloginfo('name'); if (is_single()) { // 文章页 $post = get_post(); $structured_data = [ "@context" => "https://schema.org", "@type" => "Article", "headline" => get_the_title($post), "datePublished" => get_the_date('c', $post), "dateModified" => get_the_modified_date('c', $post), "author" => [ "@type" => "Organization", "name" => $site_name ], "publisher" => [ "@type" => "Organization", "name" => $site_name, "logo" => [ "@type" => "ImageObject", "url" => get_site_icon_url() ] ], "mainEntityOfPage" => [ "@type" => "WebPage", "@id" => get_permalink($post) ] ]; } elseif (is_home() || is_front_page()) { // 首页 $structured_data = [ "@context" => "https://schema.org", "@type" => "WebSite", "name" => $site_name, "url" => $site_url, "potentialAction" => [ "@type" => "SearchAction", "target" => $site_url . '/?s={search_term_string}', "query-input" => "required name=search_term_string" ] ]; } elseif (is_category()) { // 分类页 $category = get_queried_object(); $structured_data = [ "@context" => "https://schema.org", "@type" => "CollectionPage", "name" => single_cat_title('', false), "description" => category_description($category), "url" => get_category_link($category) ]; } elseif (is_tag()) { // 标签页 $tag = get_queried_object(); $structured_data = [ "@context" => "https://schema.org", "@type" => "CollectionPage", "name" => single_tag_title('', false), "description" => tag_description($tag), "url" => get_tag_link($tag) ]; } elseif (is_search()) { // 搜索结果页 $structured_data = [ "@context" => "https://schema.org", "@type" => "SearchResultsPage", "name" => __('Search Results for: ', 'wordpress') . get_search_query(), "url" => $site_url . '/?s=' . urlencode(get_search_query()) ]; } // 输出结构化数据 if (!empty($structured_data)) { echo '<script type="application/ld+json">' . wp_json_encode($structured_data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . '</script>'; } } 附加说明 你可以在这里测试,Google官方指定的测试地址:https://validator.schema.org/# 未经允许不得转载:泥人传说 » 通过插件给WordPress自动添加结构化数据信息,简单且高效的实现方式。【亲测有效】 wordpress代码优化wordpress结构化数据wordpress结构化数据代码wordpress结构化数据插件wordpress结构化数据方法wordpress网站进行SEO优化网站SEO优化网站代码优化网站结构化数据网站结构化数据方法 分享到: 赞(0)