【小记】使用PHP为wordpress页面增加creativework结构化SEO数据


使用PHP为wordpress页面增加creativework结构化SEO数据

这个不太常用,建议还是使用普通的article更具体。


function creativework_structured_data() {
    if (is_page()) {
        // 获取当前页面的标题、内容、发布日期、修改时间等
        $page_title = get_the_title(); // 页面标题
        $page_content = get_the_content(); // 页面内容
        $page_content = strip_shortcodes($page_content); // 去除短代码
        $date_published = get_the_date('Y-m-d'); // 发布日期
        $date_modified = get_the_modified_date('Y-m-d'); // 修改日期
        $author_name = get_the_author(); // 作者名称
        $page_url = get_permalink(); // 页面URL
        $site_name = get_bloginfo('name'); // 网站名称
        $site_logo_url = get_site_icon_url(); // 网站Logo URL(如果设置了)

        // 输出结构化数据
        $structured_data = '
        <script type="application/ld+json">
        {
          "@context": "https://schema.org",
          "@type": "CreativeWork",
          "name": "' . esc_js($page_title) . '",
          "text": "' . esc_js(strip_tags($page_content)) . '", 
          "datePublished": "' . esc_js($date_published) . '",
          "dateModified": "' . esc_js($date_modified) . '",
          "author": {
           "@type": "Organization",
            "name": "你的站点名称"
          },
          "publisher": {
            "@type": "Organization",
            "name": "' . esc_js($site_name) . '",
            "logo": {
              "@type": "ImageObject",
              "url": "' . esc_url($site_logo_url) . '"
            }
          },
          "mainEntityOfPage": "' . esc_url($page_url) . '"
        }
        </script>
        ';
        
        echo $structured_data; // 使用 echo 输出数据
    }
}
add_action('wp_head', 'creativework_structured_data');

文章作者: 2winter
文章链接: https://2winter.com
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 2winter !