您可能已经注意到一些流行网站(如eHow等)如何将“阅读更多”链接附加到从其网站复制的任何文本。这是获得新用户的好方法。人们通常将网站上的文本复制并粘贴到电子邮件中。有些人用它来保存信息以供将来使用,而另一些人只是与他们的朋友、家人和同事分享。在本文中,我们将向您展示如何轻松地将“阅读更多”链接添加到WordPress博客中复制的任何文本,以便获得额外的流量。
注意:要查看实际效果,请复制本文中的所有内容并将其粘贴到您的电子邮件或记事本中。
您需要做的第一件事是将以下代码复制到主题的functions.php文件或特定于站点的插件中:
functionadd_copyright_text(){if(is_single()){?><scripttype='text/javascript'>functionaddLink(){if(window.getSelection().containsNode(document.getElementsByClassName('entry-content')[0],true)){varbody_element=document.getElementsByTagName('body')[0];varselection;selection=window.getSelection();varoldselection=selectionvarpagelink="<br/><br/>ReadmoreatWPBeginner:<?phpthe_title();?><ahref='<?phpechowp_get_shortlink(get_the_ID());?>'><?phpechowp_get_shortlink(get_the_ID());?></a>";//Changethisifyoulikevarcopy_text=selection+pagelink;varnew_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');