Wordpress: How can I add url GET parameter to my main menu items(Wordpress:如何将 url GET 参数添加到我的主菜单项)
问题描述
我正在尝试向 Wordpress 中的主菜单项之一添加 URL GET 参数(但我不知道如何操作).因此,我的方法是检测菜单项上的点击事件,然后通过 ajax 将参数传递给我的 php 页面,该页面将根据需要处理传递的值.我的主要问题是,看看我的代码,怎么不工作?有没有更好的方法在 Wordpress 中做到这一点而不依赖于 javascript?
I'm trying to add a URL GET parameter to one of my main menu items in Wordpress(but I don't know how to). So, my approach was to detect a click event on the menu item, then pass a parameter via ajax to my php page which will process value passed as needed. My main questions are, looking at my code, how come is not working? is there a better way of doing this in Wordpress and not rely on 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');        
   }
推荐答案
过滤器钩子 wp_get_nav_menu_items 用于操作导航菜单.示例中使用的 post_title 是菜单的标题(导航标签),而不是帖子/页面的标题.
The filter hook wp_get_nav_menu_items is used to manipulate the Nav Menus. The post_title used in the example is the title of the Menu (Navigation Label), not of the post/page.
将此代码放入您的 functions.php 文件中,根据您的需要调整 post_title 和 ?my_var=test.请注意,比 functions 更好的是创建自己的插件.
Drop this code in your functions.php file, adjust the post_title and ?my_var=test to your needs. Note that better than functions is to create your own plugin.
add_filter( 'wp_get_nav_menu_items','nav_items', 11, 3 );
function nav_items( $items, $menu, $args ) 
{
    if( is_admin() )
        return $items;
    foreach( $items as $item ) 
    {
        if( 'Home' == $item->post_title)
            $item->url .= '?my_var=test';
    }
    return $items;
}
                        这篇关于Wordpress:如何将 url GET 参数添加到我的主菜单项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Wordpress:如何将 url GET 参数添加到我的主菜单项
				
        
 
            
        基础教程推荐
- 将变量从树枝传递给 js 2022-01-01
 - 使用 scandir() 在目录中查找文件夹 (PHP) 2022-01-01
 - Web 服务器如何处理请求? 2021-01-01
 - 主题化 Drupal 7 的 Ubercart “/cart"页 2021-01-01
 - php中的foreach复选框POST 2021-01-01
 - PHPUnit 的 Selenium 2 文档到底在哪里? 2022-01-01
 - 如何在数学上评估像“2-1"这样的字符串?产生“1"? 2022-01-01
 - php 7.4 在写入变量中的 Twig 问题 2022-01-01
 - Yii2 - 在运行时设置邮件传输参数 2022-01-01
 - php中的PDF导出 2022-01-01
 
    	
    	
    	
    	
    	
    	
    	
    	
				
				
				
				