You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为WordPress短代码链接添加target=_blank属性且仅单次生效

解决方案

不需要修改短代码源文件,也不需要新增全局配置,仅在当前调用位置加少量内联代码即可实现,所有修改仅对本次生效,不会影响其他位置的同短代码调用。

操作步骤

  1. 在编辑器中插入「自定义HTML」块(古腾堡编辑器、经典编辑器的文本模式都支持)
  2. 粘贴如下代码,替换对应参数即可:
<!-- 自定义容器包裹本次短代码,避免影响其他元素 -->
<div class="current-box-shortcode">
[box_wide image="图片链接" text_top="Paypal" text_title="资助我们的奖学金获得者" text_description="通过PayPal向我们的奖学金基金捐款(无需注册,可直接使用银行卡支付)" link="目标跳转链接" text_align="left"]
</div>
<!-- 内联JS仅修改当前容器内的链接属性,仅当前页面生效 -->
<script>
const targetLink = document.querySelector('.current-box-shortcode a')
if (targetLink) {
  targetLink.setAttribute('target', '_blank')
  // 补充安全属性,避免新页面安全风险
  targetLink.setAttribute('rel', 'noopener noreferrer')
}
</script>

补充说明

如果短代码本次生成的内容包含多个链接,将上面的JS替换为遍历逻辑即可:

document.querySelectorAll('.current-box-shortcode a').forEach(link => {
  link.setAttribute('target', '_blank')
  link.setAttribute('rel', 'noopener noreferrer')
})

内容的提问来源于stack exchange,提问作者Márton Stark

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 18:45:05