本文整理汇总了PHP中wp_setup_nav_menu_item函数的典型用法代码示例。如果您正苦于以下问题:PHP wp_setup_nav_menu_item函数的具体用法?PHP wp_setup_nav_menu_item怎么用?PHP wp_setup_nav_menu_item使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了wp_setup_nav_menu_item函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: __construct
public function __construct($menu_term_slug)
{
parent::__construct();
$menu = wp_get_nav_menu_object($menu_term_slug);
if (!empty($menu)) {
$this->menu = $menu;
$nav_menu_items = wp_get_nav_menu_items($this->menu->term_id);
cfd_tmp_dbg('nav_menu_items_raw.txt', $nav_menu_items, 'print');
foreach ($nav_menu_items as $item) {
$menu_item = wp_setup_nav_menu_item($item);
$menu_item->metadata = get_metadata('post', $item->ID);
foreach ($menu_item->metadata as $key => &$value) {
$value[0] = maybe_unserialize($value[0]);
}
if ($menu_item->type == 'post_type') {
$menu_item->parent = get_post($menu_item->metadata['_menu_item_object_id'][0]);
} elseif ($menu_item->type == 'taxonomy' && (!property_exists($menu, 'object') || $menu->object != 'custom')) {
$menu_item->term = get_term($menu_item->metadata['_menu_item_object_id'][0], $menu_item->metadata['_menu_item_object'][0]);
}
$this->items[] = $menu_item;
}
} else {
throw new Exception(__('Invalid menu id', 'cf-deploy') . ': ' . esc_attr($menu_term_slug));
}
}
示例2: ajax_add_to_menu
/**
* Ajax callback for our menu items
*
* @wordpress-action wp_ajax_buggypress_add_to_menu
* @return void Exits the program on completion
*/
public function ajax_add_to_menu()
{
check_ajax_referer('buggypress-menu', 'buggypress_nonce');
if (empty($_POST['menu_items'])) {
die('-1');
}
require_once ABSPATH . 'wp-admin/includes/nav-menu.php';
$menu_items = $_POST['menu_items'];
$item_ids = array();
foreach ($menu_items as $item) {
$item_ids[] = $this->add_menu_item($item);
}
if (is_wp_error($item_ids)) {
die('-1');
}
// Set up menu items
$output_menu_items = array();
foreach ($item_ids as $menu_item_id) {
$menu_obj = get_post($menu_item_id);
if (!empty($menu_obj->ID)) {
$menu_obj = wp_setup_nav_menu_item($menu_obj);
$menu_obj->label = $menu_obj->title;
// don't show "(pending)" in ajax-added items
$menu_obj->type_label = 'BuggyPress';
$output_menu_items[] = $menu_obj;
}
}
// build the HTML output
if (!empty($output_menu_items)) {
$args = array('after' => '', 'before' => '', 'link_after' => '', 'link_before' => '', 'walker' => new Walker_Nav_Menu_Edit());
echo walk_nav_menu_tree($output_menu_items, 0, (object) $args);
}
exit;
}
示例3: test_orphan_nav_menu_item
/**
* @ticket 27113
*/
function test_orphan_nav_menu_item()
{
// Create an orphan nav menu item
$custom_item_id = wp_update_nav_menu_item(0, 0, array('menu-item-type' => 'custom', 'menu-item-title' => 'Wordpress.org', 'menu-item-link' => 'http://wordpress.org', 'menu-item-status' => 'publish'));
// Confirm it saved properly
$custom_item = wp_setup_nav_menu_item(get_post($custom_item_id));
$this->assertEquals('Wordpress.org', $custom_item->title);
// Update the orphan with an associated nav menu
wp_update_nav_menu_item($this->menu_id, $custom_item_id, array('menu-item-title' => 'WordPress.org'));
$menu_items = wp_get_nav_menu_items($this->menu_id);
$custom_item = wp_filter_object_list($menu_items, array('db_id' => $custom_item_id));
$custom_item = array_pop($custom_item);
$this->assertEquals('WordPress.org', $custom_item->title);
}
示例4: theme_get_list_menu
function theme_get_list_menu($args = array())
{
global $wp_query;
$menu_items = wp_get_nav_menu_items($args['menu']->term_id);
if (empty($menu_items)) {
return '';
}
$home_page_id = (int) get_option('page_for_posts');
$queried_object = $wp_query->get_queried_object();
$queried_object_id = (int) $wp_query->queried_object_id;
$active_ID = null;
$IdToKey = array();
foreach ((array) $menu_items as $key => $menu_item) {
$IdToKey[$menu_item->ID] = $key;
if ($menu_item->object_id == $queried_object_id && (!empty($home_page_id) && 'post_type' == $menu_item->type && $wp_query->is_home && $home_page_id == $menu_item->object_id || 'post_type' == $menu_item->type && $wp_query->is_singular || 'taxonomy' == $menu_item->type && ($wp_query->is_category || $wp_query->is_tag || $wp_query->is_tax))) {
$active_ID = $menu_item->ID;
} elseif ('custom' == $menu_item->object) {
if (theme_is_current_url($menu_item->url)) {
$active_ID = $menu_item->ID;
}
}
}
$current_ID = $active_ID;
while ($current_ID && isset($IdToKey[$current_ID])) {
$activeIDs[] = $current_ID;
$current_item =& $menu_items[$IdToKey[$current_ID]];
$current_item->classes[] = 'active';
$current_ID = $current_item->menu_item_parent;
}
$sorted_menu_items = array();
foreach ((array) $menu_items as $key => $menu_item) {
$sorted_menu_items[$menu_item->menu_order] = wp_setup_nav_menu_item($menu_item);
}
$items = array();
foreach ($sorted_menu_items as $el) {
$id = $el->db_id;
$title = $el->title;
//Jordi
//$classes = empty($el->classes) ? array() : (array) $el->classes;
$classes = empty($el->classes) ? array('nav') : array_merge((array) $el->classes, array('nav'));
$active = in_array('active', $classes);
$items[] = new theme_MenuItem(array('id' => $id, 'active' => $active, 'attr' => array('title' => strip_tags(empty($el->attr_title) ? $title : $el->attr_title), 'target' => $el->target, 'rel' => $el->xfn, 'href' => $el->url, 'class' => join(' ', apply_filters('nav_menu_css_class', array_filter($classes), $el))), 'title' => $title, 'parent' => $el->menu_item_parent));
}
$walker = new theme_MenuWalker();
$items = apply_filters('wp_nav_menu_objects', $items, $args);
$items = $walker->walk($items, $args);
$items = apply_filters('wp_nav_menu_items', $items, $args);
return apply_filters('wp_nav_menu', $items, $args);
}
示例5: sugarforms_menu_items
function sugarforms_menu_items($items)
{
// query sugarforms
$args = array('post_type' => 'sugarform', 'meta_key' => 'sugarform_display_menu', 'meta_value' => '1', 'meta_compare' => '=');
$loop = new WP_Query($args);
// add to menu
if ($loop->found_posts > 0) {
foreach ($loop->posts as $post) {
$new_item = wp_setup_nav_menu_item($post);
$items[] = $new_item;
//print_r($new_item);
}
}
return $items;
}
示例6: menu_customizer_add_item_ajax
/**
* Ajax handler for adding a menu item. Based on wp_ajax_add_menu_item().
*
* @since Menu Customizer 0.0.
*/
function menu_customizer_add_item_ajax()
{
check_ajax_referer('customize-menus', 'customize-menu-item-nonce');
if (!current_user_can('edit_theme_options')) {
wp_die(-1);
}
require_once ABSPATH . 'wp-admin/includes/nav-menu.php';
$menu_item_data = (array) $_POST['menu-item'];
$menu_id = absint($_POST['menu']);
// Used only for display, new item is created as an orphan - menu id of 0.
$id = 0;
// For performance reasons, we omit some object properties from the checklist.
// The following is a hacky way to restore them when adding non-custom items.
// @todo: do we really need this - do we need to populate the description field here?
if (!empty($menu_item_data['obj_type']) && 'custom' != $menu_item_data['obj_type'] && !empty($menu_item_data['id'])) {
switch ($menu_item_data['obj_type']) {
case 'post_type':
$id = absint(str_replace('post-', '', $menu_item_data['id']));
$_object = get_post($id);
break;
case 'taxonomy':
$id = absint(str_replace('term-', '', $menu_item_data['id']));
$_object = get_term($id, $menu_item_data['type']);
break;
}
$_menu_items = array_map('wp_setup_nav_menu_item', array($_object));
$_menu_item = array_shift($_menu_items);
// Restore the missing menu item properties
$menu_item_data['menu-item-description'] = $_menu_item->description;
}
// Make the "Home" item into the custom link that it actually is.
if ('page' == $menu_item_data['type'] && 'custom' == $menu_item_data['obj_type']) {
$menu_item_data['type'] = 'custom';
$menu_item_data['url'] = home_url('/');
}
// Map data from menu customizer keys to nav-menus.php keys.
$item_data = array('menu-item-db-id' => 0, 'menu-item-object-id' => $id, 'menu-item-object' => isset($menu_item_data['type']) ? $menu_item_data['type'] : '', 'menu-item-type' => isset($menu_item_data['obj_type']) ? $menu_item_data['obj_type'] : '', 'menu-item-title' => isset($menu_item_data['name']) ? $menu_item_data['name'] : '', 'menu-item-url' => isset($menu_item_data['url']) ? $menu_item_data['url'] : '', 'menu-item-description' => isset($menu_item_data['menu-item-description']) ? $menu_item_data['menu-item-description'] : '');
// `wp_save_nav_menu_items` requires `menu-item-db-id` to not be set for custom items.
if ('custom' == $item_data['menu-item-type']) {
unset($item_data['menu-item-db-id']);
}
$item_ids = wp_save_nav_menu_items(0, array(0 => $item_data));
if (is_wp_error($item_ids) || empty($item_ids)) {
wp_die(0);
}
$item = get_post($item_ids[0]);
if (!empty($item->ID)) {
$item = wp_setup_nav_menu_item($item);
$item->label = $item->title;
// Don't show "(pending)" in ajax-added items.
}
// Output the markup for this item.
menu_customizer_render_item_control($item, $menu_id, 0);
wp_die();
}
示例7: ajax_add_menu_item
/**
* Ajax handler is triggered when
* something is added to the nav menu
* @ajax
*/
public function ajax_add_menu_item()
{
if (!tf_current_user_can(array('manage_options', 'edit_theme_options'), false)) {
die('-1');
}
check_ajax_referer('add-menu_item', 'menu-settings-column-nonce');
require_once ABSPATH . 'wp-admin/includes/nav-menu.php';
// For performance reasons, we omit some object properties from the checklist.
// The following is a hacky way to restore them when adding non-custom items.
$menu_items_data = array();
if ($this->request->POST('menu-item')) {
foreach ((array) $this->request->POST('menu-item') as $menu_item_data) {
if (!empty($menu_item_data['menu-item-type']) && 'custom' != $menu_item_data['menu-item-type'] && !empty($menu_item_data['menu-item-object-id'])) {
switch ($menu_item_data['menu-item-type']) {
case 'post_type':
$_object = get_post($menu_item_data['menu-item-object-id']);
break;
case 'taxonomy':
$_object = get_term($menu_item_data['menu-item-object-id'], $menu_item_data['menu-item-object']);
break;
}
$_menu_items = array_map('wp_setup_nav_menu_item', array($_object));
$_menu_item = array_shift($_menu_items);
// Restore the missing menu item properties
$menu_item_data['menu-item-description'] = $_menu_item->description;
}
$menu_items_data[] = $menu_item_data;
}
}
$item_ids = wp_save_nav_menu_items(0, $menu_items_data);
if (is_wp_error($item_ids)) {
die('-1');
}
foreach ((array) $item_ids as $menu_item_id) {
$menu_obj = get_post($menu_item_id);
if (!empty($menu_obj->ID)) {
$menu_obj = wp_setup_nav_menu_item($menu_obj);
$menu_obj->label = $menu_obj->title;
// don't show "(pending)" in ajax-added items
$menu_items[] = $menu_obj;
}
}
if (!empty($menu_items)) {
$args = array('after' => '', 'before' => '', 'link_after' => '', 'link_before' => '', 'walker' => new TF_ADMIN_MENU_WALKER());
echo walk_nav_menu_tree($menu_items, 0, (object) $args);
}
die;
}
示例8: wp_ajax_add_menu_item
function wp_ajax_add_menu_item()
{
check_ajax_referer('add-menu_item', 'menu-settings-column-nonce');
if (!current_user_can('edit_theme_options')) {
wp_die(-1);
}
require_once ABSPATH . 'wp-admin/includes/nav-menu.php';
$menu_items_data = array();
$search_keys = array();
foreach ((array) $_POST['menu-item'] as $k => $menu_item_data) {
if (!isset($menu_item_data['menu-item-type']) || $menu_item_data['menu-item-type'] !== 'search') {
continue;
}
$menu_item_data['menu-item-description'] = _x('Search box', 'menu-item-description', 'bop-nav-search-box-item');
$menu_items_data[] = $menu_item_data;
$search_keys[] = $k;
}
foreach ($search_keys as $k) {
unset($_POST['menu-item'][$k]);
}
if (!$menu_items_data) {
return;
}
$item_ids = wp_save_nav_menu_items(0, $menu_items_data);
if (is_wp_error($item_ids)) {
wp_die(0);
}
$menu_items = array();
foreach ((array) $item_ids as $menu_item_id) {
$menu_obj = get_post($menu_item_id);
if (!empty($menu_obj->ID)) {
$menu_obj = wp_setup_nav_menu_item($menu_obj);
$menu_obj->label = $menu_obj->title;
// don't show "(pending)" in ajax-added items
$menu_items[] = $menu_obj;
}
}
/**
* This filter is defined in wp-admin/includes/nav-menu.php
*
* @since 1.0.0
*/
$walker_class_name = apply_filters('wp_edit_nav_menu_walker', 'Walker_Nav_Menu_Edit', $_POST['menu']);
if (!class_exists($walker_class_name)) {
wp_die(0);
}
if (!empty($menu_items)) {
$args = array('after' => '', 'before' => '', 'link_after' => '', 'link_before' => '', 'walker' => new $walker_class_name());
echo walk_nav_menu_tree($menu_items, 0, (object) $args);
}
if (!$_POST['menu-item']) {
wp_die();
}
}
示例9: array
$dbids_to_orders = array();
$orders_to_dbids = array();
foreach ((array) $ordered_menu_items as $ordered_menu_item_object) {
if (isset($ordered_menu_item_object->ID)) {
if (isset($ordered_menu_item_object->menu_order)) {
$dbids_to_orders[$ordered_menu_item_object->ID] = $ordered_menu_item_object->menu_order;
$orders_to_dbids[$ordered_menu_item_object->menu_order] = $ordered_menu_item_object->ID;
}
}
}
// if this menu item is not first
if (!empty($dbids_to_orders[$menu_item_id]) && !empty($orders_to_dbids[$dbids_to_orders[$menu_item_id] - 1])) {
// if this menu item is a child of the previous
if (!empty($menu_item_data['menu_item_parent']) && in_array($menu_item_data['menu_item_parent'], array_keys($dbids_to_orders)) && isset($orders_to_dbids[$dbids_to_orders[$menu_item_id] - 1]) && $menu_item_data['menu_item_parent'] == $orders_to_dbids[$dbids_to_orders[$menu_item_id] - 1]) {
$parent_db_id = in_array($menu_item_data['menu_item_parent'], $orders_to_dbids) ? (int) $menu_item_data['menu_item_parent'] : 0;
$parent_object = wp_setup_nav_menu_item(get_post($parent_db_id));
if (!is_wp_error($parent_object)) {
$parent_data = (array) $parent_object;
// if there is something before the parent and parent a child of it, make menu item a child also of it
if (!empty($dbids_to_orders[$parent_db_id]) && !empty($orders_to_dbids[$dbids_to_orders[$parent_db_id] - 1]) && !empty($parent_data['menu_item_parent'])) {
$menu_item_data['menu_item_parent'] = $parent_data['menu_item_parent'];
// else if there is something before parent and parent not a child of it, make menu item a child of that something's parent
} elseif (!empty($dbids_to_orders[$parent_db_id]) && !empty($orders_to_dbids[$dbids_to_orders[$parent_db_id] - 1])) {
$_possible_parent_id = (int) get_post_meta($orders_to_dbids[$dbids_to_orders[$parent_db_id] - 1], '_menu_item_menu_item_parent', true);
if (in_array($_possible_parent_id, array_keys($dbids_to_orders))) {
$menu_item_data['menu_item_parent'] = $_possible_parent_id;
} else {
$menu_item_data['menu_item_parent'] = 0;
}
// else there isn't something before the parent
} else {
示例10: ajax_add_post_type
/**
*
*/
function ajax_add_post_type()
{
if (!current_user_can('edit_theme_options')) {
die('-1');
}
require_once ABSPATH . 'wp-admin/includes/nav-menu.php';
if (empty($_POST['post_types'])) {
exit;
}
// Create menu items and store IDs in array
$item_ids = array();
foreach ((array) $_POST['post_types'] as $post_type) {
$post_type_obj = get_post_type_object($post_type);
if (!$post_type_obj) {
continue;
}
$menu_item_data = array('menu-item-title' => esc_attr($post_type_obj->labels->name), 'menu-item-type' => 'post_type_archive', 'menu-item-object' => esc_attr($post_type), 'menu-item-url' => get_post_type_archive_link($post_type));
//Collect the items' IDs.
$item_ids[] = wp_update_nav_menu_item(0, 0, $menu_item_data);
}
// If there was an error die here
if (is_wp_error($item_ids)) {
die('-1');
}
// Set up menu items
foreach ((array) $item_ids as $menu_item_id) {
$menu_obj = get_post($menu_item_id);
if (!empty($menu_obj->ID)) {
$menu_obj = wp_setup_nav_menu_item($menu_obj);
$menu_obj->label = $menu_obj->title;
// don't show "(pending)" in ajax-added items
$menu_items[] = $menu_obj;
}
}
// This gets the HTML to returns it to the menu
if (!empty($menu_items)) {
$args = array('after' => '', 'before' => '', 'link_after' => '', 'link_before' => '', 'walker' => new Walker_Nav_Menu_Edit());
echo walk_nav_menu_tree($menu_items, 0, (object) $args);
}
// Finally don't forget to exit
exit;
}
示例11: ajax_add_multisite_page
public function ajax_add_multisite_page()
{
switch_to_blog($_POST['blogId']);
$post = get_post($_POST['ids'][0]);
$url = get_permalink($post->ID);
restore_current_blog();
$item_ids = wp_update_nav_menu_item(0, 0, array('menu-item-title' => esc_attr($post->post_title), 'menu-item-type' => 'custom', 'menu-item-url' => $url));
foreach ((array) $item_ids as $menu_item_id) {
$menu_obj = get_post($menu_item_id);
if (!empty($menu_obj->ID)) {
$menu_obj = wp_setup_nav_menu_item($menu_obj);
// don't show "(pending)" in ajax-added items
$menu_obj->label = $menu_obj->title;
$menu_items[] = $menu_obj;
}
}
// Needed to get the Walker up and running
require_once ABSPATH . 'wp-admin/includes/nav-menu.php';
// This gets the HTML to returns it to the menu
if (!empty($menu_items)) {
$args = array('after' => '', 'before' => '', 'link_after' => '', 'link_before' => '', 'walker' => new Walker_Nav_Menu_Edit());
echo walk_nav_menu_tree($menu_items, 0, (object) $args);
}
exit;
}
示例12: test_sanitize
/**
* Test sanitize method.
*
* @see WP_Customize_Nav_Menu_Item_Setting::sanitize()
*/
function test_sanitize()
{
do_action('customize_register', $this->wp_customize);
$menu_id = wp_create_nav_menu('Primary');
$setting = new WP_Customize_Nav_Menu_Item_Setting($this->wp_customize, 'nav_menu_item[123]');
$this->assertNull($setting->sanitize('not an array'));
$this->assertNull($setting->sanitize(123));
$unsanitized = array('object_id' => 'bad', 'object' => '<b>hello</b>', 'menu_item_parent' => 'asdasd', 'position' => -123, 'type' => 'custom<b>', 'title' => '\\o/ o\'o Hi<script>unfilteredHtml()</script>', 'url' => 'javascript:alert(1)', 'target' => '" onclick="', 'attr_title' => '\\o/ o\'o <b>bolded</b><script>unfilteredHtml()</script>', 'description' => '\\o/ o\'o <b>Hello world</b><script>unfilteredHtml()</script>', 'classes' => 'hello " inject="', 'xfn' => 'hello " inject="', 'status' => 'forbidden', 'original_title' => 'Hi<script>unfilteredHtml()</script>', 'nav_menu_term_id' => 'heilo', '_invalid' => false);
$expected_sanitized = array('object_id' => 0, 'object' => 'bhellob', 'menu_item_parent' => 0, 'position' => -123, 'type' => 'customb', 'title' => current_user_can('unfiltered_html') ? '\\o/ o\'o Hi<script>unfilteredHtml()</script>' : '\\o/ o\'o HiunfilteredHtml()', 'url' => '', 'target' => 'onclick', 'attr_title' => current_user_can('unfiltered_html') ? '\\o/ o\'o <b>bolded</b><script>unfilteredHtml()</script>' : '\\o/ o\'o <b>bolded</b>unfilteredHtml()', 'description' => current_user_can('unfiltered_html') ? '\\o/ o\'o <b>Hello world</b><script>unfilteredHtml()</script>' : '\\o/ o\'o <b>Hello world</b>unfilteredHtml()', 'classes' => 'hello inject', 'xfn' => 'hello inject', 'status' => 'draft', 'original_title' => 'Hi', 'nav_menu_term_id' => 0);
$sanitized = $setting->sanitize($unsanitized);
$this->assertEqualSets(array_keys($unsanitized), array_keys($sanitized));
foreach ($expected_sanitized as $key => $value) {
$this->assertEquals($value, $sanitized[$key], "Expected {$key} to be sanitized.");
}
$nav_menu_item_id = wp_update_nav_menu_item($menu_id, 0, wp_slash(array('menu-item-object-id' => $unsanitized['object_id'], 'menu-item-object' => $unsanitized['object'], 'menu-item-parent-id' => $unsanitized['menu_item_parent'], 'menu-item-position' => $unsanitized['position'], 'menu-item-type' => $unsanitized['type'], 'menu-item-title' => $unsanitized['title'], 'menu-item-url' => $unsanitized['url'], 'menu-item-description' => $unsanitized['description'], 'menu-item-attr-title' => $unsanitized['attr_title'], 'menu-item-target' => $unsanitized['target'], 'menu-item-classes' => $unsanitized['classes'], 'menu-item-xfn' => $unsanitized['xfn'], 'menu-item-status' => $unsanitized['status'])));
$post = get_post($nav_menu_item_id);
$nav_menu_item = wp_setup_nav_menu_item(clone $post);
$this->assertEquals($expected_sanitized['object_id'], $nav_menu_item->object_id);
$this->assertEquals($expected_sanitized['object'], $nav_menu_item->object);
$this->assertEquals($expected_sanitized['menu_item_parent'], $nav_menu_item->menu_item_parent);
$this->assertEquals($expected_sanitized['position'], $post->menu_order);
$this->assertEquals($expected_sanitized['type'], $nav_menu_item->type);
$this->assertEquals($expected_sanitized['title'], $post->post_title);
$this->assertEquals($expected_sanitized['url'], $nav_menu_item->url);
$this->assertEquals($expected_sanitized['description'], $post->post_content);
$this->assertEquals($expected_sanitized['attr_title'], $post->post_excerpt);
$this->assertEquals($expected_sanitized['target'], $nav_menu_item->target);
$this->assertEquals($expected_sanitized['classes'], implode(' ', $nav_menu_item->classes));
$this->assertEquals($expected_sanitized['xfn'], $nav_menu_item->xfn);
$this->assertEquals($expected_sanitized['status'], $post->post_status);
}
示例13: test_wp_setup_nav_menu_item_for_unknown_post_type_archive_no_description
/**
* @ticket 35324
*/
function test_wp_setup_nav_menu_item_for_unknown_post_type_archive_no_description()
{
$post_type_slug = rand_str(12);
$post_type_archive_item_id = wp_update_nav_menu_item($this->menu_id, 0, array('menu-item-type' => 'post_type_archive', 'menu-item-object' => $post_type_slug, 'menu-item-status' => 'publish'));
$post_type_archive_item = wp_setup_nav_menu_item(get_post($post_type_archive_item_id));
$this->assertEmpty($post_type_archive_item->description);
}
示例14: ajax_add_year
/**
* AJAX Callback to create the menu item and add it to menu
* @return string $HTML built with walk_nav_menu_tree()
* use \Post_Type_Archive_Links::is_allowed() Check request and return choosen post types
*/
public function ajax_add_year()
{
$years = $this->is_allowed();
// Create menu items and store IDs in array
$item_ids = array();
$menu_item_data = array('menu-item-title' => esc_attr($years[0]), 'menu-item-type' => 'cyear_archive', 'menu-item-object' => esc_attr($years[0]), 'menu-item-url' => get_year_link($years[0]));
// Collect the items' IDs.
$item_ids[] = wp_update_nav_menu_item(0, 0, $menu_item_data);
// If there was an error die here
is_wp_error($item_ids) and die('-1');
// Set up menu items
foreach ((array) $item_ids as $menu_item_id) {
$menu_obj = get_post($menu_item_id);
if (!empty($menu_obj->ID)) {
$menu_obj = wp_setup_nav_menu_item($menu_obj);
// don't show "(pending)" in ajax-added items
$menu_obj->label = $menu_obj->title;
$menu_items[] = $menu_obj;
}
}
// Needed to get the Walker up and running
require_once ABSPATH . 'wp-admin/includes/nav-menu.php';
// This gets the HTML to returns it to the menu
if (!empty($menu_items)) {
$args = array('after' => '', 'before' => '', 'link_after' => '', 'link_before' => '', 'walker' => new Walker_Nav_Menu_Edit());
echo walk_nav_menu_tree($menu_items, 0, (object) $args);
}
// Finally don't forget to exit
exit;
}
示例15: test_value_nav_menu_term_id_returns_zero
/**
* Test value method returns zero for nav_menu_term_id when previewing a new menu.
*
* @see WP_Customize_Nav_Menu_Item_Setting::value()
*/
function test_value_nav_menu_term_id_returns_zero()
{
do_action('customize_register', $this->wp_customize);
$menu_id = -123;
$post_value = array('name' => 'Secondary', 'description' => '', 'parent' => 0, 'auto_add' => false);
$setting_id = "nav_menu[{$menu_id}]";
$menu = new WP_Customize_Nav_Menu_Setting($this->wp_customize, $setting_id);
$this->wp_customize->set_post_value($menu->id, $post_value);
$menu->preview();
$value = $menu->value();
$this->assertEquals($post_value, $value);
$post_id = $this->factory->post->create(array('post_title' => 'Hello World'));
$item_id = wp_update_nav_menu_item($menu_id, 0, array('menu-item-type' => 'post_type', 'menu-item-object' => 'post', 'menu-item-object-id' => $post_id, 'menu-item-title' => 'Hello World', 'menu-item-status' => 'publish'));
$post = get_post($item_id);
$menu_item = wp_setup_nav_menu_item($post);
$setting_id = "nav_menu_item[{$item_id}]";
$setting = new WP_Customize_Nav_Menu_Item_Setting($this->wp_customize, $setting_id);
$value = $setting->value();
$this->assertEquals(0, $value['nav_menu_term_id']);
}