php 文章结构化数据Wordpress

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 文章结构化数据Wordpress相关的知识,希望对你有一定的参考价值。

function json_ld_article() {
          if(is_single()) {
            global $post;
            $user_name = get_bloginfo('name');
            $article_title = get_the_title();
            $excerpt = get_the_excerpt();
            $post_author_id = (int)get_post_field( 'post_author', $post->ID );

            if( get_the_author_meta('display_name', $post_author_id ) != "" ) {
              $user_name = get_the_author_meta('display_name', $post_author_id );
            }
            else if( get_the_author_meta('user_nicename', $post_author_id) != "" ) {
              $user_name = get_the_author_meta('user_nicename', $post_author_id);
            }
            else if( get_the_author_meta('first_name', $post_author_id) != "") {
              $user_name = get_the_author_meta('first_name', $post_author_id);
            }
            else {
              $user_name = get_bloginfo('name');
            }

            if(!has_excerpt() || (trim($excerpt) == '...' || trim($excerpt) == '<p>&hellip;</p>' || trim($excerpt) == '<p>...</p>')) {
              $excerpt = cd_variable_length_excerpt(get_the_content());
            }

            $home_url = get_home_url('/');

            $excerpt = str_replace(array("\n", "\r"), '', json_encode(wp_strip_all_tags(trim($excerpt) )));

            $category_detail = get_the_category();
            $category_name = $category_detail[0]->cat_name;

            $comments_count = wp_count_comments();

            $thumbnailURL = get_the_post_thumbnail_url( get_the_ID(), 'large' );

            $post_url = get_the_permalink();

            $post_date = get_the_date('c');
            $modified_date = get_the_modified_date('c');

            $logo = '//placehold.it/512x512';

            echo '<meta property="article:author" content="'.$user_name.'" />';

            $json_ld = '<script type="application/ld+json">';
            $json_ld .=   '{';
            $json_ld .=     '"@context": "http://schema.org",';
            $json_ld .=     '"@type": "Article",';
            $json_ld .=     ' "mainEntityOfPage": {';
            $json_ld .=       ' "@type": "WebPage",';
            $json_ld .=       ' "@id": "'.$home_url.'"';
            $json_ld .=     '},';
            $json_ld .=     '"author": "'.$user_name.'",';
           $json_ld .=     ' "publisher": {';
            $json_ld .=       ' "@type": "Organization",';
            $json_ld .=       ' "name": "'.$user_name.'",';
            $json_ld .=       ' "@id": "#person",';
            $json_ld .=       ' "url": "'.$home_url.'",';
            $json_ld .=       '                  "sameAs":[ ';
            $json_ld .=       '                        "https:\/\/www.facebook.com\/user",';
            $json_ld .=       '                        "https:\/\/www.instagram.com\/user\/",';
            $json_ld .=       '                        "https:\/\/www.pinterest.com\/user\/",';
            $json_ld .=       '                        "https:\/\/twitter.com\/user"';
            $json_ld .=       '                     ],';
            $json_ld .=       ' "logo": "'.$logo.'"';
            $json_ld .=     '},';
            $json_ld .=     '"name": "'.$article_title.'",';
            $json_ld .=     '"headline": "'.$article_title.'",';
            $json_ld .=     '"articleBody": '.$excerpt.',';
            $json_ld .=     '"articleSection": "'.$category_name.'",';
            $json_ld .=     '"commentCount": '.(int)$comments_count.',';
            $json_ld .=     '"thumbnailUrl": "'.$thumbnailURL.'",';
            $json_ld .=     '"image": "'.$thumbnailURL.'",';
            $json_ld .=     '"url": "'.$post_url.'",';
            $json_ld .=     '"datePublished": "'.$post_date.'",';
            $json_ld .=     '"dateModified": "'.$modified_date.'"';
            $json_ld .=   '}';
            $json_ld .= '</script>';


            echo $json_ld;
        }
      }

      add_action('wp_head', 'json_ld_article');

以上是关于php 文章结构化数据Wordpress的主要内容,如果未能解决你的问题,请参考以下文章

php Wordpress版Google的结构化数据

php 使用谷歌结构化数据的WordPress循环

php 使用All in one SEO包的wordpress的结构化数据

php 基本Wordpress index.php的Boilerplate标记。与HTML5语义标记和Schema.org结构化数据集成。杂项其他

wordpress数据库表结构

如何给wordpress网站添加百度分享按钮或悬浮窗