将WordPress菜单项的”Target”属性更改为”iframename”。

huangapple go评论87阅读模式
英文:

Change Target attribute to iframename for WordPress Menu Items

问题

以下是翻译好的部分:

"如一些人可能知道的那样,WordPress 只允许您将菜单链接目标设置为 _blank。我正在尝试在我的 iframe 中打开菜单链接,该 iframe 具有名称="analyticsFrame"。

我已将以下代码添加到我的 functions.php 文件中,但链接仍然会重定向到 src 网站,而不是在我的 iframe "analyticsFrame" 中打开 src 链接。

function add_specific_menu_location_atts( $atts, $item, $args ) {
    // check if the item is in the primary menu
    if( $args->theme_location == 'primary' ) {
        // add the desired attributes:
        $atts['target'] = 'analyticsFrame';
    }
    return $atts;
}
add_filter( 'nav_menu_link_attributes', 'add_specific_menu_location_atts', 10, 3 );

希望这有助于您解决问题。"

英文:

As some may know, WordPress only allows you to set menu link targets to _blank. I am trying to open menu links in my iframe which has name="analyticsFrame".

I have added the following code into my functions.php file but the links are still redirecting to the src website as opposed to opening the src link inside my iframe "analyticsFrame".

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

function add_specific_menu_location_atts( $atts, $item, $args ) {
    // check if the item is in the primary menu
    if( $args-&gt;theme_location == &#39;primary&#39; ) {
      // add the desired attributes:
      $atts[&#39;target&#39;] = &#39;analyticsFrame&#39;;
    }
    return $atts;
}
add_filter( &#39;nav_menu_link_attributes&#39;, &#39;add_specific_menu_location_atts&#39;, 10, 3 );

<!-- end snippet -->

答案1

得分: 1

将您的代码添加到functions.php并在页面模板上使用以下HTML在我的测试中有效:

&lt;iframe name=&quot;analyticsFrame&quot;&gt;&lt;/iframe&gt;

确认您的iframe是否在您正在测试的页面的源代码中(这是一个简单的错误)。

英文:

Adding your code to functions.php with this HTML on a page template worked in my test:

&lt;iframe name=&quot;analyticsFrame&quot;&gt;&lt;/iframe&gt;

Confirm that your iframe is in the source code for the page you are testing on (simple mistake).

huangapple
  • 本文由 发表于 2023年6月13日 14:38:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76462232.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定