dropdown-menu-widget-thumb

Dropdown Menu – PHP Template Tag Usage Explained

on July 31, 2011

Dropdown menu widget is so easy to use as a widget, but it also supports usage of PHP template tags. Using PHP code to insert the menu, you can easily add menus to your theme as many times as you like. In this post i will give you different code snippets to add the widget to your theme.


Default usage

Adding dropdown menu widget using php is so easy. If you are going to add just one menu, things get a lot easier. All you have to do is to insert:

<?php shailan_dropdown_menu( ); ?>

somewhere in your header.php template. This is the default usage. This template tag uses settings page to get the necessary information. So after you add this code, you can easily change your menu options in the Dropdown Menu Widget options page.


Adding pages menu

To add a dropdown menu of your pages, you can use:

<?php shailan_dropdown_menu( array( 'menu' => 'pages' ) ); ?>

code. This snippet will get all the other data from the template tag options page except the menu type.


Adding categories menu

To add categories dropdown, you can use:

<?php shailan_dropdown_menu( array( 'menu' => 'categories' ) ); ?>

code. This snippet will also get all the other data from the template tag options page except the menu type.


Adding a custom menu using php code

To add a custom menu you created you need to set menu variable to either its ID or its name. Here are some examples:

<?php shailan_dropdown_menu( array('menu'=>'Katalog') ); // Adds a custom menu named Katalog ?>

<?php shailan_dropdown_menu( array('menu'=>'439') ); // Adds a custom menu with id 438 ?>

You can add as many as you like. There is no limit on the number of menus you can add.


Full arguments template

If those are not enough for your requirements here is the full arguments list. You can create many different menus using this argument template. Just change the variables as you need:

<?php // SETUP
$args =array(
	'menu' => 'pages',			// Menu type pages | categories | menuID | menu name
	'exclude' => '21,35,438',	// IDs of pages to be excluded
	'login' => true,			// Display login/logout link?
	'admin' => false,			// Show site admin link?
	'vertical' => false,		// Display vertical?
	'home' => true,				// Display home link?
	'align' => 'right',			// Align left | center | right
	'width' => '500'			// Width of menu
);
// CREATE
shailan_dropdown_menu( $args ); ?>

Conclusion

Dropdown menu widget allows you to easily manage your menus. It automatically generates menu code, effects and even CSS styles. All you need is to add it and then edit your menu as you like. Using template tag’s powers, now you can add multiple dropdown menus to your site, even if you don’t have any sidebars. Here i showed you most common uses of the template tag. I hope you found it useful.

NOTE: If you have any ideas, any points that need improvements, please leave your comment below.

ADVERTISEMENT
ADVERTISEMENT

About Shailan.com Staff

Shailan.com is a design blog about WordPress and web design. We regularly write about wordpress, plugins, themes, web design resources, php, css, jquery, adsense and more.. If you want to get notified about our posts, be sure to subscribe to our feed or follow us on facebook.

Share Your Thoughts

Your email address will not be published.

16 thoughts on “Dropdown Menu – PHP Template Tag Usage Explained”

  1. valentina says:

    I dont know what I did, but when i click on *Home* on the top menue it tells me “oops page cannot be found” I tried changing themes but it still the same. I need to fix it so when I click on *Home* it could take me or my readers to my front page or my post page. Im not trying to create static page. Can anyone help me fix this problem?

    1. Hey Valentina, seems like you are using WordPress menus. Go to Appearance > Menus to edit menu and then change target url of the menu item to fix that.

  2. Aarti says:

    Hi,

    I have 2 questions – 1.) The theme I picked in the plugin settings is white font, however, I am seeing black. Can you help with this?

    2.) How do I increase spacing between the menu columns?

    1. 1. Remove id="pagemenu" from your header.php file.
      2. You can use something like this in style.css to increase paddings of link elements:

      body div#shailan-dropdown-wrapper--1 ul.dropdown li a {
      padding: 12px 23px;
      }