• Hello,

    I have a CPT with a custom field called: special – yes/no value.

    I want to do a custom nav menu item, that i can add item with text:
    “Special product”, which dynamically add url by find the only one post have a custom field special value on “Yes”.

    So if i change a custom field in products, i want that link in menu change, with no changes in admin’s menu confugration.

    Is it possible to define my own item like “SPECIAL PRODUCT” and set this link a dynamic link produced in some function?

    I hope I explained it enough 😛

    I just want to change item link without change it in menu configuration, but by changing CPT custom value 🙂

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    Assuming your theme uses wp_nav_menu() to output the menu (usually called from header.php), then it should be possible. That function has a few filters, one of them should server your needs. Try ‘wp_nav_menu_items’, your filter callback is passed the menu HTML list content. Find the item for special product and change accordingly.

    The other question is how to determine which URL to change the menu item to. You need to do a query for that CPT where the custom field has the proper value. You can use get_posts() or create a new WP_Query object to do this. Once you get the ID of the found post, use get_the_permalink() for the URL and replace whatever is currently in the menu item with the new found permalink.

    Thread Starter Bazan

    (@bazan)

    Ive done this in some other way, dont work as good as i tried to do, but its enough in my case.

    Ive made a new static PAGE, with a template special-product.php,

    on start even before get_header(); ive made a query to get a posts with a field “special”, and save its permalink, then, if ive found a post done a wp_redirect($permalink, ‘302’);

    Else, if there isnt any “special” post it shows normal page as it is in page.php, and show its content where ive written that There is no actual special products.

    But still I would like to know how to add my custom function to prepare a menu link item to add in nav_menu in admin as i am adding normal items.

    Moderator bcworkz

    (@bcworkz)

    The menu items on the admin screen do not have the capability to understand PHP function calls. That is why you need to hook into a nav menu filter and locate that item in order to dynamically affect what the menu does.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘custom nav_menu_item function’ is closed to new replies.