WORDPRESS menu custom link settings open new window tutorial

Source: Internet
Author: User
Tags foreach php file

You can add a custom link to the WordPress custom menu, that is, set a custom url as the menu. However, the default custom link is displayed on the current page, how can I set the target = '_ blank' attribute for this link?


Find a filter in WordPress source code that can change the menu properties, at D: \ xampp \ htdocs \ wp-Des \ nav-menu-template.php:


/**
* Filter the sorted list of menu item objects before generating the menu's HTML.
 *
* @ Since 3.1.0
 *
* @ Param array $ sorted_menu_items The menu items, sorted by each menu item's menu order.
* @ Param object $ args An object containing wp_nav_menu () arguments.
*/
$ Sorted_menu_items = apply_filters ('WP _ nav_menu_objects ', $ sorted_menu_items, $ args );
The name of the filter is wp_nav_menu_objects. You can add the following code to the topic functions:

 

// Use target = '_ blank' for menu custom links'
Function add_custom_url_target_attr ($ sorted_menu_items)
{
Foreach ($ sorted_menu_items as $ menu ){
If ($ menu-> type = 'custom '){
$ Menu-> target = '_ blank ';
        }
    }
 
Return $ sorted_menu_items;
}
Add_filter ('WP _ nav_menu_objects ', 'Add _ custom_url_target_attr ');
By using the wp_nav_menu_objects hook, you can open the menu link in a new window.

In fact, we can move it to functions. the functions added in php are written to a custom plug-in. This plug-in is dedicated to our own functions. In this way, you do not need to change the theme functions. php file.

Create a folder named myfunc in the plug-in directory, and create a myfun. Php file:


<? Php
/**
* @ Package myfunc
* @ Version 1.0
*/

 
// Use target = '_ blank' for menu custom links'
Function add_custom_url_target_attr ($ sorted_menu_items)
{
Foreach ($ sorted_menu_items as $ menu ){
If ($ menu-> type = 'custom '){
$ Menu-> target = '_ blank ';
        }
    }
 
Return $ sorted_menu_items;
}
Add_filter ('WP _ nav_menu_objects ', 'Add _ custom_url_target_attr ');

This is the simplest plug-in. Pay attention to myfun. the comments in front of php must be written in this format. WordPress uses this comment to identify whether it is a plug-in entry. There are new ones that need to be added to the topic functios. php functions can be added after this file.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.