Typecho文章页面的熊掌号内容改造

尚寂新
尚寂新
2018/05/05 17:14

熊掌号
熊掌号

去年申请了熊掌号,因为当时感觉熊掌号没什么用处,于是解绑之。但发现我解绑之后,站点的手机搜索结果展现还是展现出了熊掌号的标识。为了完善一下这块的显示吧,于是决定重新弄回熊掌号。
因为实名认证什么的之前都弄过,只需要把原先的熊掌号与站点重新进行绑定就OK了。另外还发现了百度的扫脸认证,看熊掌号的几个站上推绑定脸的功能之后,绑之。

由于我本意也不想搞那些粉丝关注什么的吧,所以就没有,如果需要的话在熊掌号的后台那块的“麻辣烫粉丝改造”那块有相关的方法。MIP也不打算适配了,开站点就是为了折腾的嘛qwq,套了MIP之后还有什么意思。
下面这个是放在主题的footer.php里的(如果主题按套路写的话一般都是footer.php),放在</body>标签的上面,不用紧贴着放

<?php if ($this->is('post')) : ?>
<!--文章页的熊掌号改造-->
<script type="application/ld+json">{
    "@context": "https://ziyuan.baidu.com/contexts/cambrian.jsonld",
    "@id": "<?php $this->permalink() ?>",
     "appid": "你的熊掌号ID",
    "title": "<?php $this->title() ?>",
    "images": ["<?php  print_r(get_post_img($this));?>"],
    "pubDate": "<?php $this->date('Y-m-d\TH:i:s'); ?>"
}</script>
<?php endif; ?>

如果想要“页面”被熊掌号收录的话,上面的判断加上<?php if ($this->is('page')) : ?>的判断即可,即:

<?php if ($this->is('page')) : ?>
<!--文章页的熊掌号改造-->
<script type="application/ld+json">{
    "@context": "https://ziyuan.baidu.com/contexts/cambrian.jsonld",
    "@id": "<?php $this->permalink() ?>",
     "appid": "你的熊掌号ID",
    "title": "<?php $this->title() ?>",
    "images": ["<?php  print_r(get_post_img($this));?>"],
    "pubDate": "<?php $this->date('Y-m-d\TH:i:s'); ?>"
}</script>
<?php endif; ?>

如果想要“文章”和“页面”都被熊掌号收录的话,两个都得加上。
然后在functions.php里添加如下代码(来源于https://holmesian.org/typecho-upgrade-AMP),略改。

function get_post_img($archive)
{
    $cid = $archive->cid;
    $db = Typecho_Db::get();
    $rs = $db->fetchRow($db->select('table.contents.text')
                               ->from('table.contents')
                               ->where('cid=?', $cid));
    $text = $rs['text'];
    $pattern = '/\<img.*?src\=\"(.*?)\"[^>]*>/i';
    $patternMD = '/\!\[.*?\]\((http(s)?:\/\/.*?(jpg|png))/i';
    $patternMDfoot = '/\[.*?\]:\s*(http(s)?:\/\/.*?(jpg|png))/i';
        if (preg_match($patternMDfoot, $text, $img)) {
            $img_url = $img[1];
        } else if (preg_match($patternMD, $text, $img)) {
            $img_url = $img[1];
        } else if (preg_match($pattern, $text, $img)) {
            preg_match("/(?:\()(.*)(?:\))/i", $img[0], $result);
            $img_url = $img[1];
        } else {
            $img_url ='#';
        }
        return $img_url;
}    

然后,在header.php中添加以下script以声明熊掌号(这就有点坑了,不太想把js放在header里面,但熊掌号必须要这样放,官方说法是“紧贴</head>标签放置”)

<!-- 蛋疼的位置 熊掌号ID声明 -->
<script src="//msite.baidu.com/sdk/c.js?appid=熊掌号ID"></script>

弄完之后,可以参照这篇文章向熊掌号一股脑的推送之前发过的文章,推送完之后,基本就好了。
另外熊掌号申请原创保护,需要在近一个月内10-15篇原创文章,且这十几篇文章必须发布当天及时向熊掌号进行推送才算,以证明原创能力(好坑,本来低产似____的我也不会发多少文章,哭晕)。

已有 1 条评论 (旧评论在前)
  1. 青山
    回复
    2018-05-19 04:49 Android 4.4.4

    好多站长用熊掌号,我都没申请哈哈

添加新评论 (Markdown Supported)
(ノ°ο°)ノ