Browsed by
Tag: settings

Creating a WordPress Theme from Scratch: Adding Menus

Creating a WordPress Theme from Scratch: Adding Menus

As discussed in previous post, functions.php is automatically included in the processing of a theme. This is therefore the best place to add default settings that you want to a theme, as well as helper functions.

To make sure that a function is called on the initialisation of a theme you should use the following in the functions.php file:

function add_menus(){
// Do Stuff Here
}
add_action( ‘init’, ‘add_menus’ );

In order to register the menus for your theme you need to use: register_nav_menus with an array of ‘slugs’=>’descriptions’:

register_nav_menus(
array(
‘main_nav’ => ‘The Main Menu’,
‘footer_nav’ => ‘The Footer Menu’,
)
);

This will do two things.

1) It will add an ability to edit menus on the theme once activated:

Adding Theme Settings

 

It will also add all the menus that you have specified to the list of possible menus that you can use:
Menus of a Theme

 

Now to actually display the appropriate menu you just need to call wp_nav_menu(‘slug’).

So our example would be wp_nav_menu(‘main_nav’); or wp_nav_menu(‘footer_nav’);

This will print out whatever menu you have in those drop downs above.

However if one isn’t selected it seems to still show the menu with Home and About. So I created a wrapper in my functions.php as so:

function get_menu($menu){
$locations = get_nav_menu_locations();
if ($locations[$menu]){
wp_nav_menu(array(‘theme_location’=>$menu));
}
}

Now I simply call get_menu(‘main_nav’); and it will either show nothing if the menu is blank or it will be the actual menu selected.

That is it for menus for now. Remember to check how WordPress outputs the menus and change the CSS to suit how you want it to. (e.g. by default it uses menu-top-menu as the class). Or you can use a custom class  when calling wp_nav_menu():

wp_nav_menu( array( 'theme_location' => 'extra-menu', 'container_class' => 'my_extra_menu_class' ) );
Setting up a WordPress Blog Website (live and staging/testing versions)

Setting up a WordPress Blog Website (live and staging/testing versions)

I wanted to document the “proper” way to setup a WordPress blog on your server with a decent staging/test server.

To start off it is probably worth mentioning why WordPress is a great system to use. It is probably one of the oldest but continually updated pieces of software out there for blogs. Not only does it work well with blogs and dynamic websites but it can be used to create a static set of pages in a very easy way. There are a few pitfalls with some of the plugins that don’t seem to just work – but hey that’s what hacking away at them for hours is for right?!

First of all you can get a free wordpress blog, fully hosted at wordpress.com which is nice and easy to setup.

Alternatively you can download the latest version and upload the directory to your webserver. You will need a mySQL database to connect to with login details which you will have to input when you navigate via a browser to the directory you just uploaded those files to.

I created a live server and a staging/test server so I can play around with themes, plugins and posts without worrying about what my real users are seeing.

If you copy your live database over to your staging server you will need to update the database connection details in config.php and change a couple of rows in your database.

wp_options table: siteurl and home need to point to the correct place

I would also recommend “Discourage search engines from indexing this site” in Settings->Reading.

Perhaps also including a robots.txt file that disallows indexing of your staging site.

I also recommend using a different colour scheme on the back end.

Changing the settings on each version so I can tell by looking at the back end whether I am on the staging or live system
Changing the settings on each version so I can tell by looking at the back end whether I am on the staging or live system

This is if you can be bothered to create a test server – otherwise you would be good to go in a very short amount of time! Happy blogging!