简体   繁体   English

Wordpress 3 - 通过functions.php从帖子中删除链接

[英]Wordpress 3 - Remove Links from Posts via functions.php

Is there a way that I can remove links in posts via my functions.php file. 有没有办法可以通过我的functions.php文件删除帖子中的链接。 Basically I don't want anyone to be able to go outside of the blog posts that are viewed. 基本上我不希望任何人能够访问被查看的博客文章。 I have hundreds of posts so I obviously can't go through all of them and remove them manually. 我有数百个帖子,所以我显然无法通过所有这些帖子并手动删除它们。 Or could I use javascript? 或者我可以使用JavaScript吗?

Thanks so much. 非常感谢。


Updated: The jQuery below is great. 更新:下面的jQuery很棒。 Does anyone know if there is a way I can do it thru php in my functions.php file? 有谁知道我的functions.php文件中是否有通过php的方法? If, for whatever ridiculous reason, someone has JS disabled is why I ask. 如果,出于任何荒谬的原因,有人有JS残疾是我问的原因。

Thanks! 谢谢!

You could use JavaScript, but you're not going to be able to stop people leaving if they want to. 您可以使用JavaScript,但如果他们愿意,您将无法阻止他们离开。

Something like this may work, although I haven't tested and it was written off-hand: 像这样的东西可能会起作用,虽然我没有测试过它是手写的:

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

With the jQuery library, this should replace all <a> tags with what was in between them. 使用jQuery库,这应该用它们之间的内容替换所有<a>标签。

So <a href="http://www.google.co.uk/">Google</a> should become just Google . 因此, <a href="http://www.google.co.uk/">Google</a>应该只是Google

You can strip out the links on the fly using a regular expression - 您可以使用正则表达式即时删除链接 -

$post_content = get_the_content();
$post_content = preg_replace( "|<a *href=\"(.*)\">(.*)</a>|", "\\2", $post_content );
echo $post_content

This would need to go in your theme wherever you print the_content. 无论你在哪里打印the_content,都需要进入你的主题。 Untested. 未经测试。

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

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