【问题标题】:Make an iframe in Wordpress open links in the parent window在 Wordpress 中创建 iframe 在父窗口中打开链接
【发布时间】:2019-03-04 12:18:57
【问题描述】:

这是我的问题,我有一个带有大量链接的 wordpress 页面的 iframe(带有剥离的页眉/fooder/侧边栏),我希望当用户单击链接以在父窗口上打开而不是iframe。

我知道我可以使用: <a target="_parent" href="http://url.org">link</a> 在链接上,但由于链接指向 wordpress 帖子并且帖子列表是动态更新的,所以这不是一个很好的选择。我无法一一编辑它们以添加目标。此外,它可能会干扰网站的其他链接。

另一种选择是使用

<base target="_parent">

在 iframe 标头上,但我不知道该怎么做。 假设我有这段代码来添加 iframe:

<iframe class="bg-dark3" style="border: none;" src="https://www.google.com" scrolling="no"></iframe>

在哪里以及如何添加&lt;base target="_parent"&gt; 行?

【问题讨论】:

  • &lt;base target="_parent"&gt; 需要添加到您的链接所在的页面,即您的 wordpress 页面。可以放在&lt;head&gt;标签中
  • 你的意思是把它添加到 header.php 中?这不会影响整个网站吗?
  • 没错@Christos Arapidis。你问它应该放在哪里以及如何放置。
  • 它不起作用,因为我剥离了标题。由于该页面属于我自己的 wordpress 网站,因此我删除了页眉/页脚/侧边栏,如开头所述,这也删除了基本目标代码。还有其他选择吗?

标签: wordpress iframe


【解决方案1】:

您可以将其添加到您的 Wordpress 网站。在父级中打开链接是 Jquery。所以请确保 Jquery 库已加载。

$('a').click(function(){
  //Get url from anchor
  var link = $(this).attr('href');

  //Open url in parent
  window.parent.parent.window.location = link;
});

这将自动打开父链接中的链接,您无需手动将target=_parent 添加到每个链接。

【讨论】:

  • 我在我的functions.php中添加了以下代码:add_action( 'wp_enqueue_scripts', 'add_my_script' );然后是函数:function add_my_script() { wp_enqueue_script( 'links.js', // name your script so that you can attach other scripts and de-register, etc. get_template_directory_uri() . '/js/links.js', // this is the location of your script file array('jquery') // this array lists the scripts upon which your script depends ); }我检查了页面源并加载了js文件,但它似乎没有在iframe。
猜你喜欢
  • 2018-02-21
  • 2017-07-19
  • 1970-01-01
  • 1970-01-01
  • 2010-11-05
  • 1970-01-01
相关资源
最近更新 更多