博客使用的是hexo搭建,使用的next主题,但是配置文件中,没有百度联盟广告的配置项,只能自己搭建了

一、获取广告JS代码

我使用的是百度广告,登录百度联盟,代码位管理,创建代码位,得到JS代码,如下:

1
2
3
4
5
<script type="text/javascript">
/*580*90 创建于 2017/9/12*/
var cpro_id = "u3092140";
</script>
<script type="text/javascript" src="http://cpro.baidustatic.com/cpro/ui/c.js"></script>

二、hexo添加百度广告

新建baidu_union.swig文件

在路径\themes\next\layout\_macro中添加baidu_union.swig文件,其内容为:

1
2
3
4
5
6
7
{% if theme.baidu_union.enabled %}
<script type="text/javascript">
/*580*90 创建于 2017/9/12*/
var cpro_id = "u3092140";
</script>
<script type="text/javascript" src="http://cpro.baidustatic.com/cpro/ui/c.js"></script>
{% endif %}

修改 post.swig 文件

\themes\next\layout\_macro\post.swig中,post-body之后,post-footer之前添加如下代码:

1
2
3
4
5
<div>
{% if not is_index %}
{% include 'baidu_union.swig' %}
{% endif %}
</div>

主题配置文件增加控制字段

在主题配置文件 _config.yml中添加以下字段开启此功能:

1
2
baidu_union: 
enabled: true

完成以上设置之后,在每篇文章之后都会百度联盟广告。

效果

配置完成后,发布,能够看到如下对应效果