繁体   English   中英

需要代码从WordPress中的帖子中删除链接

[英]Need code to remove links from posts in WordPress

问题:我的文章包含其原始网站的链接以及一些指向其他网站的外部链接。 我想基于某些条件/输入删除链接,如果有与google.com的链接,则应将其删除。 我尝试了fol代码,但它删除了所有链接。

<script>
$('#content a').each(function() {
    $(this).replaceWith($(this).text());
});
</script>

我需要能够处理输入的代码。 我将在WordPress中使用它。 感谢您的时间和精力。

请使用以下代码示例

 jQuery(document).ready(function(){ jQuery('#content a').each(function() { if(jQuery(this).attr('href') == "http://optimumcreative.com/blog"){ //After this condition, it will only remove those links who have href value equals to http://optimumcreative.com/blog jQuery(this).replaceWith(jQuery(this).text()); } }); });; 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="content"> <div class="entry-content"> This is some text with <a href="http://optimumcreative.com/blog">links</a>. Some more text with <a href="http://stackoverflow.com">links </a>. </div> </div> 

您需要将jQuery代码放在document.ready块中,以便在加载页面时执行。 您可以使用wp_footer操作加载此脚本,如下所示

  function show_code_footer() {
      echo '<script>
         jQuery(document).ready(function(){
              jQuery("#content a").each(function() {
                 jQuery(this).replaceWith(jQuery(this).text());
              });
         });
      </script>';
 }
 add_action( 'wp_footer', 'show_code_footer' );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM