给WordPress增加百度的时间因子,其实就是之前熊掌号的js推送,相对会容易出图很多,有利于SEO;下面贴代码
//时间因子 function ydb_time() { if(is_single()||is_page()){ echo '<script type="application/ld+json">{ "@context": "https://ziyuan.baidu.com/contexts/cambrian.jsonld", "@id": "'.get_the_permalink().'", "appid": "秘钥", "title": "'.get_the_title().'", "images": ["'.fanly_post_imgs().'"], "description":"'.wp_trim_words(get_the_content(), 300).'", "pubDate": "'.get_the_time('Y-m-d\TH:i:s').'" } </script> ';} }; //获取文章中的图 function fanly_post_imgs(){ global $post; $src = ''; $content = $post->post_content; preg_match_all('/<img .*?src=["|\'](.+?)["|\'].*?>/', $content, $strResult, PREG_PATTERN_ORDER); $n = count($strResult[1]); if($n >= 3){ $src = $strResult[1][0].'","'.$strResult[1][1].'","'.$strResult[1][2]; }elseif($n >= 1){ $src = $strResult[1][0]; } return $src; }
然后在前端的模板的页脚处放入一下代码作为调用
<?php ydb_time(); ?>