最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Wordpress: How can I add url GET parameter to my main menu items - Stack Overflow

programmeradmin5浏览0评论

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 e 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');        
}

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 e 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');        
}
Share Improve this question edited Oct 11, 2022 at 14:35 Tom J Nowell 9,98118 gold badges66 silver badges94 bronze badges asked Sep 11, 2013 at 2:32 friedbeingsfriedbeings 851 gold badge1 silver badge6 bronze badges 2
  • This syntax is not right ({ homeclick = true }). Also I would suggest you use WordPress hooks to add what you want to the menu items. If you're using wp_nav_menu there are various hooks: wp_nav_menu, wp_nav_menu_objects, wp_nav_menu_class, etc... – elclanrs Commented Sep 11, 2013 at 2:46
  • @brasofilo thanks for you feedback. My menu item is <ul><li><a href="www.somedomain.">Home<li></ul>. I want to add a GET parameter just like you did but I don't know how to do in WP. Any examples you can share? – friedbeings Commented Sep 11, 2013 at 12:43
Add a ment  | 

4 Answers 4

Reset to default 11

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.

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;
}

I would suggest to use custom menus in Appearance>Menus. It will help you to retain custom URLs with get parameters. You may read it here Wordpress Menu

The problem is that you are attempting to pass a boolean value in your Ajax call with the variable homeclick. GET requests simply use text because the data is passed thru the URL, so if you are wanting a logical/boolean type you can use either "true" and "false" in text or perhaps 0 and 1. Also you have a syntax error, see below.

Try the following:

In you ajax call, fix the syntax as well as set homeclick to "true" as follows: data: ({ homeclick: 'true' }),.

And in your php, change the if condition for variable $homeclick as follows: $homeclick == 'true'.

You may want to consider using a POST method if you want to utilize a boolean.

That's a fork from Brasofilo that works perfectly for me: (Put this code on your theme functions.php)

// Transform title attributes to parameters
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 ($item->attr_title != "") $item->url .= '#' . $item->attr_title;
    }
    return $items;
}

What I'm doing here is to get the attributes of the menu-item and transform them to an anchor name. This way I will have this type of URL's: www.domain./my-page#anchor1

After this I just need to do some jQuery magic to jump (progressive scroll down) to the anchor. (Codepen).

If you cannot see the 'Title Attribute' input field just be sure to check the options on the top "Screen Options" button of Wordpress Admin menu...

发布评论

评论列表(0)

  1. 暂无评论