WordPress Baidu bear's paw page Transformation (pure code implementation), wordpress bear's paw
I. followers follow the transformation
1. Add the ID statement of the bear's paw
ID Declaration
1 <script src = "// msite.baidu.com/sdk/c.js? Appid = your paw ID "> </script>
There is nothing to say, just put this code in the appropriate position on your header. php page.
2. Add the Attention Function Code (highly recommended)
This will not be repeated. There are three types of ceiling bar, bar between sections in the article, and bar at the bottom, which can be added to the <body> tab of the page. wordpress is generally in footer. php. If you want to customize some styles, you can also add the following types of code:
1 <div>2 <script>cambrian.render('head')</script>3 </div>
Ii. Structural Transformation (Focus)
1. Add a canonical tag
The href content must be the PC page address corresponding to the MIP or H5 page. If there is no PC page, enter the current page address.
1 <link rel="canonical" href="http(s)://xxx"/>
Don't be alarmed when you see this. I don't know whether the address page is filled in, because wordpress is posting an article to automatically generate an address. We can't just post a new one. In fact, you just need to put the following code into your header. the php page is ready, and the article address can be automatically obtained.
1 <?php2 global $wp;3 $current_url = home_url(add_query_arg(array(),$wp->request));4 if($current_url)echo '<link rel="canonical" href="' .$current_url. '" />' . "\n" ;5 ?>
2. Add JSON_LD data (difficult)
The following code is an example of a JSON-LD:
1 <script type = "application/ld + json"> 2 {3 "@ context": "https://ziyuan.baidu.com/contexts/cambrian.jsonld", 4 "@ id": "Current webpage URL ", 5 "appid": "bear's paw ID", 6 "title": "Your Website title", 7 "images": [8 "https: // path" 9], 10 "description": "description", 11 "pubDate": "2017-06-15T08: 00: 01" 12} 13 </script>
You don't have to worry about it. The difficulty is simply how to obtain the current url, title, path of the captured image, and the description and posting date of the article. Of course, you can use the methods provided by wordpress to obtain them directly, but I have found a more perfect method for any topic. Thanks to Fan, the blog owner, it is very helpful.
The Code is as follows: Put it in your function. php.
1 // get article/page summary 2 function fanly_excerpt ($ len = 220) {3 if (is_single () | is_page () {4 global $ post; 5 if ($ post-> post_excerpt) {6 $ excerpt = $ post-> post_excerpt; 7} else {8 if (preg_match ('/<p> (. *) <\/p>/iU ', trim (strip_tags ($ post-> post_content, "<p>"), $ result )) {9 $ post_content = $ result ['1']; 10} else {11 $ post_content_r = explode ("\ n ", trim (strip_tags ($ post-> post_content); 12 $ post_content = $ p Ost_content_r ['0']; 13} 14 $ excerpt = preg_replace ('# ^ (?: [\ X00-\ x7F] | [\ xC0-\ xFF] [\ x80-\ xBF] +) {0, 0 }'.'((?: [\ X00-\ x7F] | [\ xC0-\ xFF] [\ x80-\ xBF] +) {0 ,'. $ len. '}). * # s', '$ 1', $ post_content); 15} 16 return str_replace (array ("\ r \ n", "\ r", "\ n "), "", $ excerpt); 17} 18} 19 20 // get three diagrams in the article first, otherwise, the User-Defined image, special thumbnail, and first image of the article are obtained in sequence. last update 2017/11/2321 function fanly_post_imgs () {22 global $ post; 23 $ content = $ post-> post_content; 24 preg_match_all ('/ /', $ Content, $ strResult, PREG_PATTERN_ORDER); 25 $ n = count ($ strResult [1]); 26 if ($ n> = 3) {27 $ src = $ strResult [1] [0]. '","'. $ strResult [1] [1]. '","'. $ strResult [1] [2]; 28} else {29 if ($ values = get_post_custom_values ("thumb ")) {// output custom domain image address 30 $ values = get_post_custom_values ("thumb"); 31 $ src = $ values [0]; 32} elseif (has_post_thumbnail ()) {// if there is a thumbnail, the output thumbnail address is 33 $ thumbnail_src = wp_get_attachment_image_src (get_post_thumbnail_id ($ post-> ID), 'full '); 34 $ src = $ thumbnail_src [0]; 35} else {// obtain 36 if ($ n> 0) in the article) {// extract the first figure 37 $ src = $ strResult [1] [0]; 38} 39} 40} 41 return $ src; 42}
The following code is added to header. php. The Code also adds a single page, so it only outputs information in the article.
1 <? Php 2 if (is_single () {3 echo '<script type = "application/ld + json"> {4 "@ context": "https://ziyuan.baidu.com/contexts/cambrian.jsonld ", 5 "@ id ":"'. get_the_permalink (). '", 6" appid ":" Enter the ID of the bear's paw ", 7" title ":"'. get_the_title (). '", 8" images ":["'. fanly_post_imgs (). '"], 9" description ":"'. fanly_excerpt (). '", 10" pubDate ":"'. get_the_time ('Y-m-d \ TH: I: s '). '"11 }</script> 12';} 13?>