I introduced you to "How to create a theme navigation menu in WordPress (1)". This article will continue to introduce you to how to create a theme navigation menu in WordPress. I hope it will be helpful to you!
The previous tutorial talked about how to use WordPress’s built-in functions to create navigation menus, but the HTML codes generated by these functions are fixed, making it difficult for you to define navigation. The HTML code for the menu. This article will introduce you to a few freer ways to create navigation menus that can be used for more than just navigation menus. Of course, this article only provides you with an idea to solve the problem. It is not a tutorial like a recipe. Once you read it and copy it, you can use it in your project.
1. Use get_terms() to get the category list
Use get_terms() to get your article categories, link categories and customizations Categories, etc., passing the corresponding parameters to get_terms() can return you an object array. This array is all the categories you want. The following is the function prototype of get_terms():
<?php get_terms( $taxonomies, $args ) ?>
$taxonomies:
This parameter is the classification category you want to obtain. Optional values ??include: "category", "link_category", "my_taxonomy", which respectively represent article classification, link classification and Your customized classification, where my_taxonomy is your customized classification name.
$args:
This parameter is the filtering parameter of the category, used to control the acquisition of the category you want to obtain, including how many categories you want to obtain, how to sort, and the parent category And whether to output empty categories, etc. For details, please refer to WordPress official documentation: Function Reference/get terms, or refer to the brief translation in Chinese: Common functions-get_terms()
The following is an example of using this function. Here, an unordered list in the form of
- .. ..
<ul id="menu"> <?php // 獲取分類 $terms = get_terms('category', 'orderby=name&hide_empty=0' ); // 獲取到的分類數(shù)量 $count = count($terms); if($count > 0){ // 循環(huán)輸出所有分類信息 foreach ($terms as $term) { echo '<li><a href="'.get_term_link($term, $term->slug).'" title="'.$term->name.'">'.$term->name.'</a></li>'; } } ?> </ul>
The get_terms() function returns an object array $terms. We first determine whether the array is empty. If it is empty, it means that no category has been obtained. If If it is not empty then you can output the classification. Each array item of $terms is an object. The meanings of some object attributes are: slug: category abbreviation, name: category name, term_id: category id. As shown in the above code, you can get the attribute value of the object through $term->name.
2. Use the method of reading the database to obtain the classification list
If you know the WordPress database, you can find that WordPress classification information is stored in wp_terms and wp_term_taxonomy. In the table, wp_terms stores basic information (including article classification, article tags, link classification, etc.), and wp_term_taxonomy is used to store further descriptions (used to store descriptions, distinguish categories and tags, etc.). We can use SQL to get the list of categories we want from these two tables:
<ul id="menu"> <?php $cats = $wpdb->get_results("SELECT {$wpdb->prefix}terms.term_id, name FROM {$wpdb->prefix}term_taxonomy, {$wpdb->prefix}terms WHERE {$wpdb->prefix}term_taxonomy.term_id = {$wpdb->prefix}terms.term_id AND taxonomy = 'category'"); if($cats) { foreach($cats as $cat) { echo '<li><a href="'.get_category_link($cat->term_id).'" title="'.$cat->name.'">'.$cat->name.'</a></li>'; } } ?> </ul>
3. How to get the id of the current category
Sometimes we need to make a sub-navigation, such as the human resources navigation on the left of http://www.nashowgroup.com/?p=58&lang=zh. This navigation can be any item, such as the current category subcategories under or articles under the current category, etc. So the first question is, how to get the ID of the current category so that we can take the next step.
Get the id of the current category on the category page:
if ( is_category() ) { $cat_id = get_query_var('cat'); }
Get the first category of the article on the article page:
$cats = get_the_category(); if($cats) $cat_id = $cats[0]->cat_ID;
四、子導(dǎo)航的制作
上面我們講解了如何獲取當(dāng)前分類的id,下面我們來(lái)講講如何制作子導(dǎo)航。首先,我們來(lái)制作一個(gè)當(dāng)前分類下子分類的子導(dǎo)航,這里用到wp_list_categories()來(lái)列出子分類,當(dāng)然你可以用我前面介紹的兩種方法來(lái)獲取分類。:
<ul> <?php // 這里我們用到上面獲取到的$cat_id,獲取該分類下的所有子分類 wp_list_categories('orderby=name&hide_empty=0&child_of=' . $cat_id); ?> </ul>
如果你的網(wǎng)站規(guī)模比較小,一個(gè)分類下的文章也不多,那么你可以在子導(dǎo)航中列出這個(gè)分類下的所有文章:
<ul> <?php global $wp_query; $query = array ( 'cat' => $cat_id, 'orderby' => title, 'order'=> ASC ); $queryObject = new WP_Query($query); if ($queryObject->have_posts()) : while ($queryObject->have_posts()) : $queryObject->the_post(); ?> <li><a <?php if($post->ID == $wp_query->post->ID) echo 'class="chose"'; ?> href="<?php the_permalink() ?>"><?php the_title(); ?></a></li> <?php endwhile; wp_reset_postdata(); endif; ?> </ul>
以上代碼中用到了WP_Query來(lái)獲取文章列表,該對(duì)象的使用方法,可以參考WordPress的官方文檔:Class Reference/WP Query和Function Reference/query posts。class="chose"用于高亮當(dāng)前文章的菜單項(xiàng),css規(guī)則你可以自己定義。
五、頁(yè)面page的獲取
WordPress的頁(yè)面page可以通過(guò)wp_list_pages()來(lái)列出,不過(guò)這個(gè)函數(shù)輸出的HTML都是固定的,如果你想要自定義這些HTML,可以使用get_pages()來(lái)獲取頁(yè)面列表,代碼示例如下:
<ul id="menu"> $mypages = get_pages(); if(count($mypages) > 0) { foreach($mypages as $page) { echo '<li><a href="'.get_page_link($page->ID).'" title="'.$page->post_title.'">'.$page->post_title.'</a></li>'; } } else { echo '<li><a href="#">沒(méi)有頁(yè)面</a></li>'; } </ul>
-- 完 --
推薦學(xué)習(xí):《WordPress教程》
The above is the detailed content of How to create a theme navigation menu in WordPress (2). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

User voice input is captured and sent to the PHP backend through the MediaRecorder API of the front-end JavaScript; 2. PHP saves the audio as a temporary file and calls STTAPI (such as Google or Baidu voice recognition) to convert it into text; 3. PHP sends the text to an AI service (such as OpenAIGPT) to obtain intelligent reply; 4. PHP then calls TTSAPI (such as Baidu or Google voice synthesis) to convert the reply to a voice file; 5. PHP streams the voice file back to the front-end to play, completing interaction. The entire process is dominated by PHP to ensure seamless connection between all links.

The core method of building social sharing functions in PHP is to dynamically generate sharing links that meet the requirements of each platform. 1. First get the current page or specified URL and article information; 2. Use urlencode to encode the parameters; 3. Splice and generate sharing links according to the protocols of each platform; 4. Display links on the front end for users to click and share; 5. Dynamically generate OG tags on the page to optimize sharing content display; 6. Be sure to escape user input to prevent XSS attacks. This method does not require complex authentication, has low maintenance costs, and is suitable for most content sharing needs.

To realize text error correction and syntax optimization with AI, you need to follow the following steps: 1. Select a suitable AI model or API, such as Baidu, Tencent API or open source NLP library; 2. Call the API through PHP's curl or Guzzle and process the return results; 3. Display error correction information in the application and allow users to choose whether to adopt it; 4. Use php-l and PHP_CodeSniffer for syntax detection and code optimization; 5. Continuously collect feedback and update the model or rules to improve the effect. When choosing AIAPI, focus on evaluating accuracy, response speed, price and support for PHP. Code optimization should follow PSR specifications, use cache reasonably, avoid circular queries, review code regularly, and use X

1. Maximizing the commercial value of the comment system requires combining native advertising precise delivery, user paid value-added services (such as uploading pictures, top-up comments), influence incentive mechanism based on comment quality, and compliance anonymous data insight monetization; 2. The audit strategy should adopt a combination of pre-audit dynamic keyword filtering and user reporting mechanisms, supplemented by comment quality rating to achieve content hierarchical exposure; 3. Anti-brushing requires the construction of multi-layer defense: reCAPTCHAv3 sensorless verification, Honeypot honeypot field recognition robot, IP and timestamp frequency limit prevents watering, and content pattern recognition marks suspicious comments, and continuously iterate to deal with attacks.

PHP does not directly perform AI image processing, but integrates through APIs, because it is good at web development rather than computing-intensive tasks. API integration can achieve professional division of labor, reduce costs, and improve efficiency; 2. Integrating key technologies include using Guzzle or cURL to send HTTP requests, JSON data encoding and decoding, API key security authentication, asynchronous queue processing time-consuming tasks, robust error handling and retry mechanism, image storage and display; 3. Common challenges include API cost out of control, uncontrollable generation results, poor user experience, security risks and difficult data management. The response strategies are setting user quotas and caches, providing propt guidance and multi-picture selection, asynchronous notifications and progress prompts, key environment variable storage and content audit, and cloud storage.

PHP ensures inventory deduction atomicity through database transactions and FORUPDATE row locks to prevent high concurrent overselling; 2. Multi-platform inventory consistency depends on centralized management and event-driven synchronization, combining API/Webhook notifications and message queues to ensure reliable data transmission; 3. The alarm mechanism should set low inventory, zero/negative inventory, unsalable sales, replenishment cycles and abnormal fluctuations strategies in different scenarios, and select DingTalk, SMS or Email Responsible Persons according to the urgency, and the alarm information must be complete and clear to achieve business adaptation and rapid response.

PHPisstillrelevantinmodernenterpriseenvironments.1.ModernPHP(7.xand8.x)offersperformancegains,stricttyping,JITcompilation,andmodernsyntax,makingitsuitableforlarge-scaleapplications.2.PHPintegrateseffectivelyinhybridarchitectures,servingasanAPIgateway

Select the appropriate AI voice recognition service and integrate PHPSDK; 2. Use PHP to call ffmpeg to convert recordings into API-required formats (such as wav); 3. Upload files to cloud storage and call API asynchronous recognition; 4. Analyze JSON results and organize text using NLP technology; 5. Generate Word or Markdown documents to complete the automation of meeting records. The entire process needs to ensure data encryption, access control and compliance to ensure privacy and security.
