【发布时间】:2013-09-14 22:05:33
【问题描述】:
我正在尝试将 URL GET 参数添加到我在 Wordpress 中的主菜单项之一(但我不知道如何)。因此,我的方法是检测菜单项上的单击事件,然后通过 ajax 将参数传递给我的 php 页面,该页面将根据需要处理传递的值。我的主要问题是,看看我的代码,为什么不工作?有没有更好的方法在 Wordpress 中做到这一点而不依赖于 javascript?
Here is the javascript:
<script type="text/javascript">
$(document).ready(function() {
$("#menu-item-128").click(function() {
$.ajax({
url: 'homepage.php',
type: "GET",
data: ({ homeclick = true }),
success: function() {
alert("success!");
}
});
});
});
</script>
Here is my PHP:
$homeclick = $_GET['homeclick'];
if ( !isset( $_COOKIE['hs_user'] ) ) {
get_header();
} elseif (isset( $_COOKIE['hs_user'] ) && $homeclick == true ) {
get_header();
} else {
// Do Something else
header('Location: homepage-returning-users');
}
【问题讨论】:
-
这个语法不对
({ homeclick = true })。另外,我建议您使用 WordPress 挂钩将您想要的内容添加到菜单项中。如果您使用wp_nav_menu,则有各种钩子:wp_nav_menu、wp_nav_menu_objects、wp_nav_menu_class等... -
@brasofilo 感谢您的反馈。我的菜单项是 。我想像你一样添加一个 GET 参数,但我不知道如何在 WP 中做。有什么可以分享的例子吗?
标签: php javascript wordpress menu parameters