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.