900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > php target标签 为Typecho文章页url标签添加nofollow和target属性

php target标签 为Typecho文章页url标签添加nofollow和target属性

时间:2019-11-24 13:40:47

相关推荐

php  target标签 为Typecho文章页url标签添加nofollow和target属性

Typecho默认的a标签是只有href属性的,也就是覆盖本页打开。

但是大多数情况下我们都希望链接从新窗口或新标签页打开。

默认html

下面是默认的html代码。

千思网

希望的html

页内链接不仅要从新窗口打开,而且从seo方面考虑,我们还希望为站外的url添加nofollow属性。

那么要达到上述的效果,html代码应该是下面这样的,增加了target="_blank" rel="nofollow"。

千思网

变更typecho代码

要达到上述效果,可以直接更改typecho的php代码。

修改/var/CommonMark/HtmlRenderer.php文件。

修改前

下面是修改前的代码104 case CommonMark_Element_InlineElement::TYPE_LINK:

105 $attrs['href'] = $this->escape($inline->getAttribute('destination'), true);

106 if ($title = $inline->getAttribute('title')) {

107 $attrs['title'] = $this->escape($title, true);

108 }

修改后

下面是修改后的代码,请将107行中替换为你的域名。104 case CommonMark_Element_InlineElement::TYPE_LINK:

105 $attrs['href'] = $this->escape($inline->getAttribute('destination'), true);

106 $attrs['target'] = $this->escape("_blank", true);

107 if ( !substr_count($attrs['href'],"") ) {

108 $attrs['rel'] = $this->escape("nofollow", true);

109 }

110 if ($title = $inline->getAttribute('title')) {

111 $attrs['title'] = $this->escape($title, true);

112 }

最终效果

指定域下面的连接不加nofollow,站外链接加nofollow。

千思网

百度

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。