WordPress函数文档add_node()

向工具栏加入自定义链接 描述 This function is a method of the $wp_adm…

向工具栏加入自定义链接

描述

This function is a method of the $wp_admin_bar global object (see: WP_Admin_Bar), and can be used to add a new item to the Toolbar. You can also use it to change the properties of an item that is already in the Toolbar.

The Toolbar replaces the Admin Bar since WordPress Version 3.3.

Toolbar items are also called “nodes”. Nodes can be parents for other nodes, which creates dropdown menus. Group nodes together with add_group() to create distinct sections in a Toolbar menu.

Note: This function is a method of the WP_Admin_Bar class and $wp_admin_bar global object, which may not exist except during the ‘admin_bar_menu’ or ‘wp_before_admin_bar_render’ hooks.

Note: add_menu is an alias for this method.

New as of Wednesday, June 17, 2015, see code for sorting the menu items on the admin bar.

用法

<?php $wp_admin_bar->add_node$args ); ?>

参数

$args

(array) (必填) An array of arguments.

默认值: None

Arguments

id

(string) (必填) The ID of the node.

默认值: false

title

(string) (可选) The text that will be visible in the Toolbar. Including html tags is allowed.

默认值: false

parent

(string) (可选) The ID of the parent node.

默认值: false

href

(string) (可选) The ‘href’ attribute for the link. If ‘href’ is not set the node will be a text node.

默认值: false

group

(boolean) (可选) This will make the node a group (node) if set to ‘true’. Group nodes are not visible in the Toolbar, but nodes added to it are. See add_group().

默认值: false

meta

(array) (可选) An array of meta data for the node.

默认值: array()

  • ‘html’ – The html used for the node.
  • ‘class’ – The class attribute for the list item containing the link or text node.
  • ‘rel’ – The rel attribute.
  • ‘onclick’ – The onclick attribute for the link. This will only be set if the ‘href’ argument is present.
  • ‘target’ – The target attribute for the link. This will only be set if the ‘href’ argument is present.
  • ‘title’ – The title attribute. Will be set to the link or to a div containing a text node.
  • ‘tabindex’ – The tabindex attribute. Will be set to the link or to a div containing a text node.

示例

Add a Page to the Toolbar

This example will add a Page with a “my-toolbar-page” class to the Toolbar. It will be a top level item because the ‘parent’ argument is not set (it has no parent node). Put this in your theme’s functions.php file.

Make an Existing Child Node a Parent Node

This example will change the properties of an existing node by using the ID of that node. See Finding Toolbar Node ID’s on how to find existing node ID’s. The following code will make the child node with ID “new-post” (New > Post) a parent node.

Single function to add parent/child sub menu items

This is how to add a parent or sub menu item with one function.

Sorting The Links on the Admin Menu Bar

Use further array functions in PHP to sort the admin menu items with the following code.

Problem:
Since the basic code for admin bar links is to create a separate $args variable for each array, sorting the links could be a lot of work cutting and pasting code around when you need to sort the links.

Solution:
The following code does the following:
1. adds each $args into a separate element of the same $args by using PHP’s array_push() function
2. Each $args element is a multi-dimensional array that can be sorted
3. Use PHP’s sort function to sort them.
4. Traverse a for loop.

源文件

add_node() 函数的代码位于 wp-includes/class-wp-admin-bar.php.

相关

Toolbar API

  • Article(文章): The WordPress Toolbar
  • Class(类): WP_Admin_Bar
  • Function(函数): add_node()
  • Function(函数): remove_node()
  • Function(函数): add_menu()
  • Function(函数): remove_menu()
  • Function(函数): add_group()
  • Function(函数): get_node()
  • Function(函数): get_nodes()
  • 原文:http://codex.wordpress.org/Function_Reference/add_node
类别:WordPress函数文档

本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。

评论 (0)COMMENT