使用Obsidian的WordPress插件时,为WordPress里面的所有链接添加新窗口 或者 新标签页 打开页面的代码 _blank

在Obsidian中用markdown建立的链接是没办法到html的_blank标签的,导致通过Wordpress插件把文单同步到Wordpress时,所有链接就是在当前页面打开,而不重新开一个标签页,解决这个问题的办法简单粗暴,就是为所有链接都加上 target="_blank", 以 Blocksy 主题为例 找到主题的 functions.php文件,然后在里面添加代码.

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

代码添加好后如下

<?php
/**
 * Blocksy functions and definitions
 *
 * @link https://developer.wordpress.org/themes/basics/theme-functions/
 *
 * @package Blocksy
 */

if (version_compare(PHP_VERSION, '5.7.0', '<')) {
	require get_template_directory() . '/inc/php-fallback.php';
	return;
}

require get_template_directory() . '/inc/init.php';

/**
 * 下面的为要添加的代码
 */

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

现在随便点哪个链接,就会在浏览器的新标签页打开

留下评论

您的电子邮箱地址不会被公开。 必填项已用 * 标注

− 3 = 6