How Do I Add a Button to the Header Menu in WordPress?
Adding a button to the header menu in WordPress is easy. First, you need to create a new file called header.
php and place it in the WordPress theme folder. Then, add the following code to the header.php file:.
post_type)) { $header = ‘
‘ . $post->post_type . ‘
‘; } else { $header = ‘
Posts
‘; } $header .= ‘
- ‘; $sub_menu = array(); foreach ($posts as $post) { $sub_menu[] = array( ‘post-type’ => $post->post_type, ‘menu-item-name’ => $post->post_title, ‘menu-item-url’ => get_permalink($post->ID), ‘sub-menu-item-class’ => ‘sub-menu-item’, ‘sub-menu-item-image’ => $post->post_image, ‘sub-menu-item-status’ => ‘publish’, ); } $header .= ‘
- ‘ . $sub_menu[ ‘menu-item-name’ ] . ‘
‘ .
$header . ‘
‘; } function add_footer() { $footer = ”; if (isset($post->post_type)) { $footer = ‘
‘; } else { $footer = ‘
- ‘; $sub_menu = array(); foreach ($posts as $post) { $sub_menu[] = array( ‘post-type’ => $post->post_type, ‘menu-item-name’ => $post->post_title, ‘menu-item-url’ => get_permalink($post->ID), ‘sub-menu-item-class’ => ‘sub-menu-item’, ‘sub-menu-item-image’ => $post->post_image, ‘sub-menu-item-status’ => ‘publish’, ); } $footer . $footer . ‘
‘; } ?>.
In the header.php file, you first need to add the add_action() function calls for adding the header and footer. Next, you need to set some global variables. The first variable is $post, which is an object containing information about the current post. The second variable is $header, which is an empty string until you define it.
The next line of code checks to see if the post type is set and, if so, sets the $header variable to the appropriate string. Next, you loop through all of the posts in the posts array and create a sub-menu for each post type. For each post, you add the post title, the post URL, the class for the sub-menu item, the image for the menu item, and the publish/unpublish status for the menu item. Finally, you add the sub-menu items to the $footer variable.
When you’re finished, you need to save the header.php file and upload it to your WordPress site. Then, you need to add a link to the header.
php file in the WordPress admin area. To do this, open the WordPress admin area, go to the header menu, and click the Add New Link button. Enter the following code into the text box:.
http://mysite.com/header.php
When you click the Add New Link button, WordPress will redirect you to the header.php file on your site.
Click the OK button to return to the header menu. Now, you can add a button to the header menu and use it to add new posts to your blog.
5 Related Question Answers Found
How Do I Add an Image to My Header Menu in WordPress?
How Do I Add a Header to Text in WordPress?
How Do I Add a Scrolling Header in WordPress?
How Do I Add the Top Menu Header in WordPress?
How Do I Add a Call Button to My WordPress Header?
Related Posts
- What Is WordPress Website Designing? When someone thinks of website design, they likely think of a professional who
- How Do I Use a CDN Image in WordPress? A CDN (content delivery network) is a network of servers around the world
- How Do I Create a Custom Validation in WordPress? Creating a custom validation in WordPress is fairly simple. You can find the