简体   繁体   English

用Wordpress替换href内的“ .html”

[英]replacing “.html” inside a href with Wordpress

I have a wordpress site with lots of links in it. 我有一个WordPress站点,其中有很多链接。 Every link has the extension ".html" (like <a href="https://example.com/about.html> ). 每个链接都有扩展名“ .html”(例如<a href="https://example.com/about.html> )。

I like to add a filter which searches for the ".html" and replaces it with "" ( <a href="https://example.com/about> ) on all pages. 我想添加一个过滤器,该过滤器在所有页面上搜索“ .html”并将其替换为“”( <a href="https://example.com/about> )。

I have tried to get this done with str_ireplace and the gettext filter. 我尝试使用str_ireplace和gettext过滤器来完成此操作。

function kb_rename_links( $kb_rename_item ) 
{  
$kb_rename_item = str_ireplace( '.html', '', $kb_rename_item );

return $kb_rename_item;
}

add_filter( 'gettext', 'kb_rename_links' );

This snipped doesn't do the job (it works for plain text). 此片段无法完成工作(它适用于纯文本)。 I think the gettext filter isn't the right one for this job or even i need a action here. 我认为gettext过滤器不是此工作的正确选择,甚至我需要在此处执行操作。 But i am to noobish to find the right one. 但是我不愿找到合适的人。

Thanks for your help 谢谢你的帮助

I think a better solution would be to modify the .htaccess file to remove the .html extensions. 我认为更好的解决方案是修改.htaccess文件以删除.html扩展名。 This way you don't have to touch the code at all. 这样,您根本不需要接触代码。

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]

source 资源

Edit: Here is a similar stackoverflow post to my solution. 编辑: 是我的解决方案类似的stackoverflow帖子。

尝试使用“ the_content”过滤器,而不是ot gettext

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

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