WordPress博客实现自动加入版权声明和链接


现在写个博客很不容易,还被抄袭或者转载,于是很多博主在文章底部会加入版权声明和链接。希望有些尊重版权的互联网人能在转载的时候给留个链接和出处。但是如果每篇文章都手动加入的话会很麻烦,而WordPress博客可以很容易的实现自动加入版权声明和链接。

原来LiboSEO使用的是直接在文章模版里的文章下面添加代码,我使用的是WordPress官方的twentyten主题修改的,所以编辑主题里的loop-single.php,找到下面这段代码:

<div class="entry-content">
<?php the_content(); ?>
<?php wp_link_pages( array( 'before' => '<div class="page-link">' . __( 'Pages:', 'twentyten' ), 'after' => '</div>' ) ); ?>
</div><!-- .entry-content -->

<?php the_content(); ?>

后面直接加入下面的代码:

<pre>转载请注明来自<a href='http://***.com'>逍遥博客@LiboSEO</a>,本文地址:<a href=<?php the_permalink(); ?>' title='<?php the_title(); ?>'><?php the_permalink(); ?></a>
除非注明,逍遥博客文章均为原创,转载请注明出处和链接!</pre>

但是出了个问题,就是如果安装了一些wumii或者百度分享之类的插件的话,上面添加的内容只能显示在无觅之类的下面,而不是紧紧贴着文章。显然这个不是我们要的效果。

经过各种测试,终于通过主题的自定义函数解决了。

方法很简单,因为之所以我们添加的内容不能紧贴着文章,就是因为这些插件将内容插入到了the_content();函数里,而这个函数是WordPress程序默认的函数。我们如果直接修改the_content();函数,那么如果升级WordPress程序的话,就会被覆盖。

于是我通过在主题的functions.php文件,在最下面添加了一个自定义的函数liboseo_content();,内容如下:

function liboseo_content($more_link_text = null, $stripteaser = 0) {
$content = get_the_content($more_link_text, $stripteaser);
$content.= "<pre>转载请注明来自<a href='http://***.com'>逍遥博客@LiboSEO</a>,";
$content.= "本文地址:<a href='".get_permalink($post, true)."' title='".get_the_title($post_id)."'>".get_permalink($post, true)."</a>";
$content.= "\n除非注明,逍遥博客文章均为原创,转载请注明出处和链接!</pre>";
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]&gt;', $content);
echo $content;

然后把主题中的文章模版里的the_content();替换成自定义的函数,比如我用的主题模版文件是loop-single.php,就直接将the_content();修改成liboseo_content();,变成了:

<div class="entry-content">
<?php liboseo_content(); ?>
<?php wp_link_pages( array( 'before' => '<div class="page-link">' . __( 'Pages:', 'twentyten' ), 'after' => '</div>' ) ); ?>
</div><!-- .entry-content -->

保存一下看看吧,是不是成功了?修改之前一定要备份原来的文件,如果可能的话,最好在本地测试好之后,再在网站上修改。


« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3