WordPress优化-文章内链接默认在新窗口打开是怎么实现的

方法一

WordPress主题的 functions.php 的最后一个 ?> 前添加下面的代码即可所有文章内链接地址自动在新窗口打开

function autoblank($text) {
 $return = str_replace('<a', '<a target="_blank"', $text);
 return $return;
 }
 add_filter('the_content', 'autoblank');

方法二

在你 WordPress 主题的 functions.php 文件中添加如下代码:

add_filter('the_content','the_content_blank',999);
function the_content_blank($content) {
    preg_match_all('/<a(.*?)href="(.*?)"(.*?)>/',$content,$matches);
    if($matches){
        foreach($matches[2] as $val){
            if(strpos($val, "#")===false && !preg_match('/\.(jpg|jepg|png|ico|bmp|gif|tiff)/i',$val)) {
                $content = str_replace( "href=\"".$val."\"", " target=\"_blank\" "."href=\"".$val."\"", $content );
            }
        }
    }
    return $content;
}

相关文章

评论 (0)