當前位置:首頁>WordPress建站>WordPress開發(fā)>WordPress添加自定義字段到菜單項

WordPress添加自定義字段到菜單項

在《WordPress 5.4可將自定義字段添加到菜單項》中,我們簡單介紹了添加自定義字段到菜單項的鉤子,但是其中的例子太簡單了。今天看到朋友評論問到:如何添加輸入框字段。倡萌Google了一下,找到了 Nav Menu Roles 插件作者的一篇文章,里面完整介紹了一個例子。

配圖并不是本示例代碼的實際效果

從WordPress5.4開始,新增了wp_nav_menu_item_custom_fields 鉤子,讓我們可以通過這個鉤子添加自定義字段到菜單項。

注意:本文代碼只能在 WordPress 5.4+ 的版本中才可用。

添加自定義字段

首先,我們將回調(diào)函數(shù)附加到新的鉤子上,并使用它來顯示文本輸入框。因為Walker顯示了多個菜單項,所以我們必須確保輸入內(nèi)容是菜單項的ID:

/**
* Add custom fields to menu item
*
* This will allow us to play nicely with any other plugin that is adding the same hook
*
* @param  int $item_id 
* @params obj $item - the menu item
* @params array $args
*/
function kia_custom_fields( $item_id, $item ) {

	wp_nonce_field( 'custom_menu_meta_nonce', '_custom_menu_meta_nonce_name' );
	$custom_menu_meta = get_post_meta( $item_id, '_custom_menu_meta', true );
	?>

	<input type="hidden" name="custom-menu-meta-nonce" value="<?php echo wp_create_nonce( 'custom-menu-meta-name' ); ?>" />

	<div class="field-custom_menu_meta description-wide" style="margin: 5px 0;">
	    <span class="description"><?php _e( "Extra Field", 'custom-menu-meta' ); ?></span>
	    <br />

	    <input type="hidden" class="nav-menu-id" value="<?php echo $item_id ;?>" />

	    <div class="logged-input-holder">
	        <input type="text" name="custom_menu_meta[<?php echo $item_id ;?>]" id="custom-menu-meta-for-<?php echo $item_id ;?>" value="<?php echo esc_attr( $custom_menu_meta ); ?>" />
	        <label for="custom-menu-meta-for-<?php echo $item_id ;?>">
	            <?php _e( 'Custom menu text', 'custom-menu-meta'); ?>
	        </label>
	    </div>

	</div>

	<?php
}
add_action( 'wp_nav_menu_item_custom_fields', 'kia_custom_fields', 10, 2 );

注意:上面的示例用的字段id是 _custom_menu_meta,請注意根據(jù)你的實際需要去修改字段的id。下文的代碼也一樣記得修改為統(tǒng)一的字段id。

保存字段設置

由于菜單項是WordPress的一種自定義文章類型,因此處理數(shù)據(jù)與添加和檢索常規(guī)文章或頁面的post_meta相同。

/**
* Save the menu item meta
* 
* @param int $menu_id
* @param int $menu_item_db_id	
*/
function kia_nav_update( $menu_id, $menu_item_db_id ) {

	// Verify this came from our screen and with proper authorization.
	if ( ! isset( $_POST['_custom_menu_meta_nonce_name'] ) || ! wp_verify_nonce( $_POST['_custom_menu_meta_nonce_name'], 'custom_menu_meta_nonce' ) ) {
		return $menu_id;
	}

	if ( isset( $_POST['custom_menu_meta'][$menu_item_db_id]  ) ) {
		$sanitized_data = sanitize_text_field( $_POST['custom_menu_meta'][$menu_item_db_id] );
		update_post_meta( $menu_item_db_id, '_custom_menu_meta', $sanitized_data );
	} else {
		delete_post_meta( $menu_item_db_id, '_custom_menu_meta' );
	}
}
add_action( 'wp_update_nav_menu_item', 'kia_nav_update', 10, 2 );

如何調(diào)用自定義字段的數(shù)據(jù)

上面的數(shù)據(jù)可以保存在數(shù)據(jù)中了,那我們該如何調(diào)用呢?

其實,這個和文章的meta是一樣的,畢竟菜單就是一種自定義文章類型。我們可以通過get_post_meta()函數(shù)獲取,比如:

get_post_meta( $item->ID, '_custom_menu_meta', true );

相信做過WP開發(fā)的人,就非常熟悉這個函數(shù)了。

下面是一個簡單的示例,將我們這個例子的字段值,用作標題后面的一個描述文字:

/**
* Displays text on the front-end.
*
* @param string   $title The menu item's title.
* @param WP_Post  $item  The current menu item.
* @return string      
*/
function kia_custom_menu_title( $title, $item ) {

	if( is_object( $item ) && isset( $item->ID ) ) {

		$custom_menu_meta = get_post_meta( $item->ID, '_custom_menu_meta', true );

		if ( ! empty( $custom_menu_meta ) ) {
			$title .= ' - ' . $custom_menu_meta;
		}
	}
	return $title;
}
add_filter( 'nav_menu_item_title', 'kia_custom_menu_title', 10, 2 );

總結

只要你始終記得 菜單一種自定義文章類型,那很多邏輯就比較好理解了,畢竟作為WP開發(fā)者,對文章的自定義字段應該比較熟悉。

以上就是一個很好的例子,代碼參考出自:https://www.kathyisawesome.com/add-custom-fields-to-wordpress-menu-items/

聲明:本站所有文章,如無特殊說明或標注,均為本站原創(chuàng)發(fā)布。任何個人或組織,在未征得本站同意時,禁止復制、盜用、采集、發(fā)布本站內(nèi)容到任何網(wǎng)站、書籍等各類媒體平臺。如若本站內(nèi)容侵犯了原著者的合法權益,可聯(lián)系我們進行處理。

給TA打賞
共{{data.count}}人
人已打賞
歡迎關注WordPress大學公眾號 WPDAXUE
WordPress開發(fā)

WordPress自定義古騰堡編輯器的顏色調(diào)色板

2020-4-19 10:29:48

WordPress開發(fā)

WordPress通過Cookie記錄用戶的搜索歷史

2020-4-21 10:03:36

3 條回復 A文章作者 M管理員
  1. 吾柯

    太感謝wordpress大學了!!!真的昨天我提的問題想不到官方能專門寫文章解答,我很意外。當時我用了谷歌但沒搜到很尷尬。
    謝謝解答!謝謝!祝wordpress大學越做越好!

  2. 阿宏

    學習學習

?
個人中心
購物車
優(yōu)惠劵
今日簽到
有新私信 私信列表
搜索

内丘县| 博兴县| 鲁山县| 固阳县| 舟山市| 冀州市| 镇江市| 江门市| 惠水县| 久治县| 万全县| 桦南县| 兰考县| 洛宁县| 明溪县| 万载县| 宜州市| 资源县| 永川市| 丁青县| 麟游县| 安康市| 西畴县| 贵定县| 诏安县| 福建省| 静安区| 海城市| 乌拉特中旗| 花莲市| 柳江县| 南丰县| 芮城县| 盐山县| 隆安县| 嘉定区| 琼结县| 辰溪县| 富阳市| 天津市| 富锦市|