最新消息:本站所有跳转向bbs.ykit.cn的附件将全面停止,附件已转移到https://www.qingsj.com

WordPress内容被复制后自动添加文章链接

wordpress教程 有客 1656浏览 0评论

WordPress站点的内容是经常被人复制的,如果你希望有这么一个功能可以在被复制文章添加你的链接的话。

那么有客来告诉你,

将下面的代码添加到主题的 functions.php 文件中,在别人复制你文章的时候会自动在文章后面添加链接!试试看吧!

function add_copyright_text() {
    if (is_single()) { ?>
 
<script type='text/javascript'>
function addLink() {
    if (
window.getSelection().containsNode(
document.getElementsByClassName('entry-content')[0], true)) {
    var body_element = document.getElementsByTagName('body')[0];
    var selection;
    selection = window.getSelection();
    var oldselection = selection
    var pagelink = "<br /><br /> 阅读更多: <?php the_title(); ?> <a href='<?php echo get_permalink(get_the_ID()); ?>'><?php echo get_permalink(get_the_ID()); ?></a>"; //根据你的需要修改这行代码
    var copy_text = selection + pagelink;
    var new_div = document.createElement('div');
    new_div.style.left='-99999px';
    new_div.style.position='absolute';
 
    body_element.appendChild(new_div );
    new_div.innerHTML = copy_text ;
    selection.selectAllChildren(new_div );
    window.setTimeout(function() {
        body_element.removeChild(new_div );
    },0);
}
}
 
 
document.oncopy = addLink;
</script>
 
<?php
}
}
 
add_action( 'wp_head', 'add_copyright_text');

 

转载请注明:有客帮 » WordPress内容被复制后自动添加文章链接

您必须 登录 才能发表评论!