wordpress文章底部自動加入版權(quán)聲明和鏈接

字號:


    現(xiàn)在寫個博客很不容易,還被抄襲或者轉(zhuǎn)載,于是很多博主在文章底部會加入版權(quán)聲明和鏈接。希望有些尊重版權(quán)的互聯(lián)網(wǎng)人能在轉(zhuǎn)載的時候給留個鏈接和出處。但是如果每篇文章都手動加入的話會很麻煩,而wordpress博客可以很容易的實現(xiàn)自動加入版權(quán)聲明和鏈接。
    原來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>轉(zhuǎn)載請注明來自<a href='http://***.com'>逍遙博客@liboseo</a>,本文地址:<a href=<?php the_permalink(); ?>' title='<?php the_title(); ?>'><?php the_permalink(); ?></a>
    除非注明,逍遙博客文章均為原創(chuàng),轉(zhuǎn)載請注明出處和鏈接!</pre>
    但是出了個問題,就是如果安裝了一些wumii或者百度分享之類的插件的話,上面添加的內(nèi)容只能顯示在無覓之類的下面,而不是緊緊貼著文章。顯然這個不是我們要的效果。
    經(jīng)過各種測試,終于通過主題的自定義函數(shù)解決了。
    方法很簡單,因為之所以我們添加的內(nèi)容不能緊貼著文章,就是因為這些插件將內(nèi)容插入到了the_content();函數(shù)里,而這個函數(shù)是 wordpress程序默認的函數(shù)。我們?nèi)绻苯有薷膖he_content();函數(shù),那么如果升級wordpress程序的話,就會被覆蓋。
    于是我通過在主題的functions.php文件,在最下面添加了一個自定義的函數(shù)liboseo_content();,內(nèi)容如下:
    function liboseo_content($more_link_text = null, $stripteaser = 0) {
    $content = get_the_content($more_link_text, $stripteaser);
    $content.= <pre>轉(zhuǎn)載請注明來自<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除非注明,逍遙博客文章均為原創(chuàng),轉(zhuǎn)載請注明出處和鏈接!</pre>;
    $content = apply_filters('the_content', $content);
    $content = str_replace(']]>', ']]&gt;', $content);
    echo $content;
    然后把主題中的文章模版里的the_content();替換成自定義的函數(shù),比如我用的主題模版文件是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 -->
    保存一下看看吧,是不是成功了?修改之前一定要備份原來的文件,如果可能的話,最好在本地測試好之后,再在網(wǎng)站上修改。