Home Backend Development PHP Tutorial PHP 学习笔记 (4)

PHP 学习笔记 (4)

Jun 13, 2016 pm 12:28 PM
class gt menu

PHP 学习笔记 (四)

Wordpress 框架中的一些函数:

wp_get_nav_menu($default): 根据条件$default 获取menu, 默认获取所有的menu

其中 $default 默认如下所示:

<span style="color: #800080;">$default</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">(  ‘theme_location’ </span>=>'',  'menu' =>'',  'container' =>'div',  'container_class' => 'menu-{menu_slug}-container',  'container_id' =>'',  'menu_class'=>'menu',  'echo' => <span style="color: #0000ff;">true</span>,  'fallback_cb'=>'wp_page_menu',  'before' =>'',  'after' =>'',  'link_before' =>'',  'link_after' =>'',  'items_wrap' =>'
Copy after login
    %3$s
, 'depth' =>0, 'walker' =>'');

参数的说明:

$theme_location: 默认为none, 指定注册过的某一个导航菜单,若未指定,则显示第一个。 如primary, secondary等

$menu:默认为none, 使用导航菜单的名称调用菜单,可以使 term_id, slug, name 等属性

$container: 默认为 div, 是ul的父节点,只支持div 和nav标签,也可用false 去掉ul 父节点的标签

$container_class: 默认为menu-{menu_slug}-container, 是ul 父节点的class属性

$container_id: 默认为none , ul父节点的id

$menu_class: 默认为menu, ul节点的class属性

$menu_id: 默认为menu slug, 自增长的ul节点的id属性值

$echo: 默认为true 确定直接显示导航菜单韩式返回HTML片段,如果想将导航的代码作为肤质使用,可以设置为false.

$fallback_db: 默认值为wp_page_menu, 用于没有在后台设置导航时调的回调函数

$items_wrap: 默认为none, 使用字符串修改替换ul的class

$depth: 默认为0, 显示菜单的层级深度, 为0时显示所有深度的菜单

$walker: 默认使用 new Walker_Nav_Menu(), 自定义的遍历对象,调用一个Walker子类对象定义显示导航菜单。

 

使用Walker 子类制作自定义菜单:

Walker 类是一个抽象类,该类定义了如何显示导航等,这个抽象类包含4个属性和6个方法:

属性:

$tree_type  , $db_fields, $max_pages, $has_children

方法:

start_lvl: 当Walker类达到结构顶层时执行该方法,可用于输出开始元素

end_lvl: 关闭start_lvl输出的元素标签

start_el: 为每一个元素输出一个开始标签

end_el: 关闭start_el 的标签

(核心)display_element: 定义如何显示树形结构中的元素

(核心)walk 根据$max_depth, 循环树形结构中的每一个元素

 

如果需要自定义导航,可以直接写一个类类继承Walker, 但是这样要重写所有的方法。如果改动不大,可以直接继承Walker_NAV_Menu类,重写需要修改的类即可。

在重写 display_element时, 参数$element 包含当前遍历到的元素的所有信息, 如可以用$element->object_id 来获取当前元素的term_id

 

代码片段: 使用add_shorcode('name','function_name') 可在系统中产生一个代码片段 ['name'], 可以在wordpress的后台文章中直接使用,或者在代码中使用 show_shortcode['[name]']; 直接执行

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the differences between Huawei GT3 Pro and GT4? What are the differences between Huawei GT3 Pro and GT4? Dec 29, 2023 pm 02:27 PM

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

How to use classes and methods in Python How to use classes and methods in Python Apr 21, 2023 pm 02:28 PM

Concepts and instances of classes and methods Class (Class): used to describe a collection of objects with the same properties and methods. It defines the properties and methods common to every object in the collection. Objects are instances of classes. Method: Function defined in the class. Class construction method __init__(): The class has a special method (construction method) named init(), which is automatically called when the class is instantiated. Instance variables: In the declaration of a class, attributes are represented by variables. Such variables are called instance variables. An instance variable is a variable modified with self. Instantiation: Create an instance of a class, a specific object of the class. Inheritance: that is, a derived class (derivedclass) inherits the base class (baseclass)

Fix: Snipping tool not working in Windows 11 Fix: Snipping tool not working in Windows 11 Aug 24, 2023 am 09:48 AM

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

Replace the class name of an element using jQuery Replace the class name of an element using jQuery Feb 24, 2024 pm 11:03 PM

jQuery is a classic JavaScript library that is widely used in web development. It simplifies operations such as handling events, manipulating DOM elements, and performing animations on web pages. When using jQuery, you often encounter situations where you need to replace the class name of an element. This article will introduce some practical methods and specific code examples. 1. Use the removeClass() and addClass() methods jQuery provides the removeClass() method for deletion

What does class mean in python? What does class mean in python? May 21, 2019 pm 05:10 PM

Class is a keyword in Python, used to define a class. The method of defining a class: add a space after class and then add the class name; class name rules: capitalize the first letter. If there are multiple words, use camel case naming, such as [class Dog()].

Detailed explanation of PHP Class usage: Make your code clearer and easier to read Detailed explanation of PHP Class usage: Make your code clearer and easier to read Mar 10, 2024 pm 12:03 PM

When writing PHP code, using classes is a very common practice. By using classes, we can encapsulate related functions and data in a single unit, making the code clearer, easier to read, and easier to maintain. This article will introduce the usage of PHPClass in detail and provide specific code examples to help readers better understand how to apply classes to optimize code in actual projects. 1. Create and use classes In PHP, you can use the keyword class to define a class and define properties and methods in the class.

Vue error: Unable to use v-bind to bind class and style correctly, how to solve it? Vue error: Unable to use v-bind to bind class and style correctly, how to solve it? Aug 26, 2023 pm 10:58 PM

Vue error: Unable to use v-bind to bind class and style correctly, how to solve it? In Vue development, we often use the v-bind instruction to dynamically bind class and style, but sometimes we may encounter some problems, such as being unable to correctly use v-bind to bind class and style. In this article, I will explain the cause of this problem and provide you with a solution. First, let’s understand the v-bind directive. v-bind is used to bind V

How to Fix Can't Connect to App Store Error on iPhone How to Fix Can't Connect to App Store Error on iPhone Jul 29, 2023 am 08:22 AM

Part 1: Initial Troubleshooting Steps Checking Apple’s System Status: Before delving into complex solutions, let’s start with the basics. The problem may not lie with your device; Apple's servers may be down. Visit Apple's System Status page to see if the AppStore is working properly. If there's a problem, all you can do is wait for Apple to fix it. Check your internet connection: Make sure you have a stable internet connection as the "Unable to connect to AppStore" issue can sometimes be attributed to a poor connection. Try switching between Wi-Fi and mobile data or resetting network settings (General > Reset > Reset Network Settings > Settings). Update your iOS version:

See all articles