Enhance navigation in your WordPress theme: Convert static HTML
Aug 30, 2023 pm 05:29 PM If you have followed this series, you now have a working theme with template files uploaded to WordPress. In this tutorial, you'll continue working with the header.php
file you created in Part 2. You will learn how to add a navigation menu that is editable through the WordPress menu admin screen. To do this, you will also need to create a new file for your theme: the function file.
What do you need
To complete this tutorial you will need the following:
- Code Editor of Your Choice
- Browser for testing your work
- Local or remote WordPress installation
- If you are working locally, you will need MAMP, WAMP, or LAMP to run WordPress.
- If you work remotely, you will need to access your website via FTP and have an admin account on your WordPress installation.
1.Registration Navigation Menu
To register a navigation menu, use the register_nav_menu()
function, which you need to add to your theme's functions.php
file.
Since your theme doesn't already have this file, you'll want to create one first.
In the theme folder, create a new blank file named functions.php
.
Open the new file and add the following content to it:
<?php function wptutsplus_register_theme_menu() { register_nav_menu( 'primary', 'Main Navigation Menu' ); } add_action( 'init', 'wptutsplus_register_theme_menu' ); ?>
You just created your theme’s first feature, so give yourself a pat on the back!
The function you created is named wptutsplus_register_theme_menu()
, I added the wptutsplus
prefix to the beginning of its name to ensure that the name is unique and does not match any of the registered Other functions conflict through plugins you may be running on your site.
This function includes register_nav_menu()
WordPress function, used to create menus. Your function will then be activated via the init
action hook, which means WordPress will run your function on initialization.
NOTE: You must activate such functions via the correct hook, otherwise they will not work.
register_nav_menu()
The function has two parameters:
- One of these parameters includes the location of the menu. In this example, we'll call that location
'primary'
. You will add this to yourheader.php
file later so WordPress displays the correct menu. - The second parameter is the description of the menu. In this case,
'Main Navigation Menu'
. This will be visible in the Menu admin screen.
2. Set navigation menu
You now have access to the Menu dashboard screen, which was previously unavailable because your theme did not have a registered menu. It's not perfect at the moment, but we'll change it soon:



As you create pages, posts, and other content, you can add them to the navigation menu from this screen. I will add two new pages called "Blog" and "About". I will specify the Blog page as the page where my posts will appear via the Settings screen. You can create any page you like.
After completing this operation, return to the "Menu" screen to edit the menu and add a new page. After dragging the new page to the menu, click Create Menu to create the new menu.
Finally, check "Main Navigation Menu" under "Theme Location" to ensure that the menu will appear as the main menu you just registered and save the menu.



NOTE: Always remember to save your menu after making any changes to it - unlike widgets, WordPress does not automatically save menus.
3. 將菜單添加到您的主題
目前,此菜單在您的網(wǎng)站上仍然不可見;您需要將菜單添加到頭文件中才能實(shí)現(xiàn)此目的。
打開主題的 header.php
文件并找到以下代碼:
<nav class="menu main"> <?php /* Allow screen readers / text browsers to skip the navigation menu and get right to the good stuff */ ?> <div class="skip-link screen-reader-text"> <a title="Skip to content" href="#content">Skip to content</a> </div> <ul> <li> <a href="#">Home</a> </li> <li> <a href="#">Latest news</a> </li> <li> <a href="#">Featured articles</a> </li> </ul> </nav><!-- .main -->
并將其替換為:
<nav class="menu main"> <?php /* Allow screen readers / text browsers to skip the navigation menu and get right to the good stuff */ ?> <div class="skip-link screen-reader-text"> <a title="Skip to content" href="#content">Skip to content</a> </div> <?php wp_nav_menu( array( 'container_class' => 'main-nav', 'theme_location' => 'primary' ) ); ?> </nav><!-- .main -->
這將添加您在主題中的此位置注冊(cè)的導(dǎo)航菜單,使用 wp_nav_menu()
函數(shù)并指定 'primary'
(您在注冊(cè)時(shí)為菜單指定的位置)為'主題位置'
。
這現(xiàn)在反映在我網(wǎng)站的導(dǎo)航菜單中:



摘要:菜單不僅僅用于網(wǎng)站標(biāo)題!
在本教程中,您學(xué)習(xí)了如何注冊(cè)導(dǎo)航菜單、向其中添加項(xiàng)目以及將其添加到網(wǎng)站標(biāo)題。
需要注意的是,菜單不僅僅必須位于網(wǎng)站標(biāo)題中。您可以在多個(gè)位置添加菜單,包括:
- 側(cè)邊欄 - 可能是網(wǎng)站某個(gè)部分的部分菜單或當(dāng)前頁面的子頁面列表
- 頁腳 - “小字體”頁面或最常訪問的頁面的菜單。
- 在主導(dǎo)航下方 - 也許是主導(dǎo)航下方的部分菜單。
您可以通過三種方式之一在主題中的更多位置添加菜單。
我按照難度升序列出了它們:
- 通過“菜單”管理屏幕創(chuàng)建額外的菜單,然后使用“自定義菜單”小部件將它們顯示在主題中具有小部件區(qū)域的任何位置
- 通過“菜單”管理屏幕創(chuàng)建額外的菜單,然后將它們添加到主題的代碼中,如上面所做的那樣。在本例中,您向
wp_nav_menu()
函數(shù)調(diào)用的數(shù)組添加一個(gè)附加參數(shù),指定'menu'
參數(shù)作為您為創(chuàng)建的每個(gè)菜單指定的名稱。 - 使用
register_nav_menus()
函數(shù)注冊(cè)多個(gè)菜單,并將它們添加到主題中的相關(guān)位置,如上所述
為什么不嘗試一下呢?
資源
- 導(dǎo)航菜單指南
-
register_nav_menu()
函數(shù) -
wp_nav_menu()
函數(shù)
The above is the detailed content of Enhance navigation in your WordPress theme: Convert static HTML. 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)

The main reasons why WordPress causes the surge in server CPU usage include plug-in problems, inefficient database query, poor quality of theme code, or surge in traffic. 1. First, confirm whether it is a high load caused by WordPress through top, htop or control panel tools; 2. Enter troubleshooting mode to gradually enable plug-ins to troubleshoot performance bottlenecks, use QueryMonitor to analyze the plug-in execution and delete or replace inefficient plug-ins; 3. Install cache plug-ins, clean up redundant data, analyze slow query logs to optimize the database; 4. Check whether the topic has problems such as overloading content, complex queries, or lack of caching mechanisms. It is recommended to use standard topic tests to compare and optimize the code logic. Follow the above steps to check and solve the location and solve the problem one by one.

Miniving JavaScript files can improve WordPress website loading speed by removing blanks, comments, and useless code. 1. Use cache plug-ins that support merge compression, such as W3TotalCache, enable and select compression mode in the "Minify" option; 2. Use a dedicated compression plug-in such as FastVelocityMinify to provide more granular control; 3. Manually compress JS files and upload them through FTP, suitable for users familiar with development tools. Note that some themes or plug-in scripts may conflict with the compression function, and you need to thoroughly test the website functions after activation.

Methods to optimize WordPress sites that do not rely on plug-ins include: 1. Use lightweight themes, such as Astra or GeneratePress, to avoid pile-up themes; 2. Manually compress and merge CSS and JS files to reduce HTTP requests; 3. Optimize images before uploading, use WebP format and control file size; 4. Configure.htaccess to enable browser cache, and connect to CDN to improve static resource loading speed; 5. Limit article revisions and regularly clean database redundant data.

TransientsAPI is a built-in tool in WordPress for temporarily storing automatic expiration data. Its core functions are set_transient, get_transient and delete_transient. Compared with OptionsAPI, transients supports setting time of survival (TTL), which is suitable for scenarios such as cache API request results and complex computing data. When using it, you need to pay attention to the uniqueness of key naming and namespace, cache "lazy deletion" mechanism, and the issue that may not last in the object cache environment. Typical application scenarios include reducing external request frequency, controlling code execution rhythm, and improving page loading performance.

The most effective way to prevent comment spam is to automatically identify and intercept it through programmatic means. 1. Use verification code mechanisms (such as Googler CAPTCHA or hCaptcha) to effectively distinguish between humans and robots, especially suitable for public websites; 2. Set hidden fields (Honeypot technology), and use robots to automatically fill in features to identify spam comments without affecting user experience; 3. Check the blacklist of comment content keywords, filter spam information through sensitive word matching, and pay attention to avoid misjudgment; 4. Judge the frequency and source IP of comments, limit the number of submissions per unit time and establish a blacklist; 5. Use third-party anti-spam services (such as Akismet, Cloudflare) to improve identification accuracy. Can be based on the website

When developing Gutenberg blocks, the correct method of enqueue assets includes: 1. Use register_block_type to specify the paths of editor_script, editor_style and style; 2. Register resources through wp_register_script and wp_register_style in functions.php or plug-in, and set the correct dependencies and versions; 3. Configure the build tool to output the appropriate module format and ensure that the path is consistent; 4. Control the loading logic of the front-end style through add_theme_support or enqueue_block_assets to ensure that the loading logic of the front-end style is ensured.

To add custom user fields, you need to select the extension method according to the platform and pay attention to data verification and permission control. Common practices include: 1. Use additional tables or key-value pairs of the database to store information; 2. Add input boxes to the front end and integrate with the back end; 3. Constrain format checks and access permissions for sensitive data; 4. Update interfaces and templates to support new field display and editing, while taking into account mobile adaptation and user experience.

robots.txt is crucial to the SEO of WordPress websites, and can guide search engines to crawl behavior, avoid duplicate content and improve efficiency. 1. Block system paths such as /wp-admin/ and /wp-includes/, but avoid accidentally blocking the /uploads/ directory; 2. Add Sitemap paths such as Sitemap: https://yourdomain.com/sitemap.xml to help search engines quickly discover site maps; 3. Limit /page/ and URLs with parameters to reduce crawler waste, but be careful not to block important archive pages; 4. Avoid common mistakes such as accidentally blocking the entire site, cache plug-in affecting updates, and ignoring the matching of mobile terminals and subdomains.
