WordPress如何给文章内容中的指定关键词添加链接

追格官方小助手/ 2022年10月03日/ WordPress/ 浏览 1056

早些时候追格小编分享过一篇《WordPress怎么给文章关键词标签自动添加内链》,今天我们说说如何给文章内容中的指定关键词添加链接。


方法和前面介绍的一致,我们只需在WordPress主题目录下的functions.php文件中添加以下代码就可以实现:


function zhuige_replace_tag($text){
	$replace = array(
		'关键字A' => '<a href="https://www.zhuige.com" target="_blank">关键字A</a>',
		'关键字B' => '<a href="https://www.zhuige.com" target="_blank">关键字B</a>',
		'关键字C' => '<a href="https://www.zhuige.com" target="_blank" >关键字C</a>'
	);
	$text = str_replace(array_keys($replace), $replace, $text);
		return $text;
	}

add_filter('the_content', 'zhuige_replace_tag');


以上关键词和链接,按需修改即可。


相关推荐:
WordPress用代码实现自动为文章添加标签

发表评论

暂无评论,抢个沙发...

客服 工单