英文:
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->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 );
<!-- end snippet -->
答案1
得分: 1
将您的代码添加到functions.php
并在页面模板上使用以下HTML在我的测试中有效:
<iframe name="analyticsFrame"></iframe>
确认您的iframe是否在您正在测试的页面的源代码中(这是一个简单的错误)。
英文:
Adding your code to functions.php
with this HTML on a page template worked in my test:
<iframe name="analyticsFrame"></iframe>
Confirm that your iframe is in the source code for the page you are testing on (simple mistake).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论