[wordpress插件] Custom Menu Fields自定义菜单字段

wordpress 插件 文章 2020-04-08 18:10 462 0 全屏看文

AI助手支持GPT4.0

评分
100
描述

This plugin provides an API which allows the developer of a site to add custom fields on the default menu editor.

此插件提供了一个API,该API允许网站的开发人员在默认菜单编辑器上添加自定义字段。

Example Initialisation

示例初始化

  <?php

add_action('init', 'menu_excerpt__add_menu_field');

add_action('init','menu_excerpt__add_menu_field');

function menu_excerpt__add_menu_field() {    

函数menu_excerpt__add_menu_field(){    

if (!is_callable('bh_add_custom_menu_fields'))        

如果(!is_callable('bh_add_custom_menu_fields'))        

return;    

返回;    

bh_add_custom_menu_fields(array(        

bh_add_custom_menu_fields(array(        

'excerpt' => array(            

'摘录'=>数组(            

'description' => 'Excerpt',            

'描述'=>'摘录',            

'type' => 'textarea',            

'type'=>'textarea',            

)));

)));

}

}

?>

?>

Accessing the fields

访问字段

The easiest way to access the field(s) you’ve added is to use something along the lines of:

访问您添加的字段的最简单方法是使用类似于以下内容的内容:

$menu = 'menuName';

  $ menu ='menuName';

$posts = wp_get_nav_menu_items($menu);

$ posts = wp_get_nav_menu_items($ menu);

foreach ($posts as $p) {    

foreach($ posts as $ p){    

$myitem = get_post_meta($p->ID, '_menu_item_youritem', true);    

$ myitem = get_post_meta($ p-> ID,'_menu_item_youritem',true);    

// do with $myitem what you like - it should be a string,    

//用$ myitem做你喜欢的-它应该是一个字符串,    

// so the simplest thing is to "echo" it

//因此,最简单的方法是“回显”它

}

}

you can use menu locations to get the menu name if you prefer – replace the first line above with:

如果愿意,可以使用菜单位置获取菜单名称-将上面的第一行替换为:

$locations = get_nav_menu_locations();

  $ locations = get_nav_menu_locations();

$menu = $locations['locationName'];

$ menu = $ locations ['locationName'];

menu locations are useful if you like to swap your menus about.

如果想要交换菜单,

菜单位置会很有用。

Unfortunately these examples don’t allow the use of wordpress’ inbuilt menu walkers.

不幸的是,这些示例不允许使用wordpress的内置菜单浏览器。

To use those you will need to create a custom walker_nav_menu class and access the custom fields with something along the lines of (very stripped down example will need fleshing out for full walker functionality – there are tutorials on the net for custom nav walkers):<

要使用这些功能,您将需要创建一个自定义的walker_nav_menu类,并使用类似于的内容访问自定义字段(非常简单的示例将需要充实才能获得完整的沃克功能-网上有自定义导航沃克的教程):<

/p>

/ p>

class mywalker extends Walker_Nav_Menu {    

  class mywalker扩展了Walker_Nav_Menu {    

function start_el(&$output, $item, $depth, $args) {        

函数start_el(&$ output,$ item,$ depth,$ args){        

echo $item->custom_field    

回声$ item-> custom_field    

}

}

}

}

The important bit here is that the field is placed on the second variable which behaves like an object.

这里重要的一点是,该字段位于行为类似于对象的第二个变量上。

the custom_field part is the name you gave your field with dashes replaced with underscores (to allow the name to be used in an accessor).

custom_field部分是您为字段指定的名称,并用短划线代替了下划线(以便在访问器中使用该名称)。

NOTE

注意

This plugin does nothing by itself.

此插件本身不执行任何操作。

It provides an API only.

它仅提供API。

安装步骤

This section describes how to install the plugin and get it working.

本节介绍如何安装插件并使其正常工作。

    1. Upload the folder from the zip to the /wp-content/plugins/ directory
    2. 从zip文件夹将文件夹上传到 / wp-content / plugins / 目录

    3. Activate the plugin through the ‘Plugins’ menu in WordPress
    4. 通过WordPress中的“插件”菜单激活插件

    5. Once activated the API is available in the ‘init’ and later phases of the WordPress lifecycle.
    6. 一旦激活,API就会在WordPress生命周期的“初始”阶段和更高阶段可用。

下载地址
https://downloads.wordpress.org/plugin/custom-menu-fields.0.2.zip
-EOF-

AI助手支持GPT4.0