Useful WordPress snippets you should know
Here are some very useful wordpress snippets you should have in your tool box to save you some head scratching time.
1. Making a unique home index page
Lets say for example we want our wordpress front page to display a set number of posts and have something like an image gallery slider at the top of this page. If we did this using index.php then that image gallery slider would appear on every index listings page as the category listings, search etc all read off index.php.
If we want our image gallery slider just to appear on the front page then we need to copy and paste all the code in index.php into a new document, and name this new document home.php so that wordpress will now use this as a dynamic front page
At the top of you new home.php file just add the following.
1 2 3 4 | <?php get_header(); query_posts('posts_per_page=()'); //returns only the front page ?> |
Notice the line that says
1 | query_posts('posts_per_page=()'); //returns only the front page |
I have set the posts per page to ( ) as this way it can be set as usual in the settings > reading menu of the admin section. Think of it in similar terms to when you create an array in javascript and we populate the array with a blank value as we don’t know how many items we are going to populate that array with.
You could if you wish change it to:
1 | query_posts('posts_per_page=3'); //returns only the front page |
Which would output only 3 posts on the home page, the choice is up to you what method you choose. You can then add whatever you choose in home.php file to add your custom content, it could be a widget, an intro message, a gallery – anything.
2. Why is my admin section going blank after I post/edit something?
I’ve seen this happen myself when making themes from scratch and 10 times out of 10 it’s because of carriage returns after the closing ?> in functions.php.
Locate the following at the bottom of functions.php
1 | ?> |
Take out any whitespace after this and make sure there are no carriage returns, this will fix that problem, really simple fix and a good one to always remember.
3. My newly installed plug-ins just ‘don’t work’
Another really common problem with home brewed themes is that some people just code up the header.php like regular html and just chuck in a few template tags etc.. This is a sure fire way or borking your plug ins (especially those that use a javascript library) as there is nothing being inserted in the <head> to make these calls. To combat this you must always use this at the top of your header.php file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en"> <head> <meta charset="<?php bloginfo( 'charset' ); ?>" /> <meta name="viewport" content="width=device-width" /> <title> <?php // Print the <title> tag based on what is being viewed. global $page, $paged; wp_title( '|', true, 'right' ); // Add the blog name. bloginfo( 'name' ); // Add the blog description for the home/front page. $site_description = get_bloginfo( 'description', 'display' ); if ( $site_description && ( is_home() || is_front_page() ) ) echo " | $site_description"; ?></title> <link rel="profile" href="http://gmpg.org/xfn/11" /> <link rel="stylesheet" type="text/css" media="all" href="<?php bloginfo( 'stylesheet_url' ); ?>" /> <link rel="pingback" href="<?php bloginfo( 'pingback_url' ); ?>" /> <?php wp_head(); ?> </head> |
Notice I have placed inside the <title> the following
1 2 | <?php ?> |
And also just before the closing </head> tag:
1 2 3 | <?php wp_head(); ?> </head> |
4. Setting up widget areas
The great thing about wordpress is that you can make almost any area of your site a widgetized area. Here is a simple way of making an area at the top of that home.php file we set up earlier.
First lets choose a name for our widget, lets call this ‘welcome-text’. Open up a new document and name this welcome-text.php and paste in the following code then save it in your wordpress theme directory:
1 2 3 4 | <?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('welcome-text') ) : ?> <?php if ( is_home() | is_archive() | is_single() ) : ?> <?php endif // is_home() | is_archive() ?> <?php endif; ?> |
Next open up functions.php – here we need to define the new widget area:
1 2 3 4 5 6 7 | register_sidebar(array('name'=>'welcome-text', 'before_widget' => '', 'after_widget' => '', 'description' => 'This is the intro text at the top of the home page', 'before_title' => '', 'after_title' => '', )); |
All we are doing here is
- Defining the widget area’s name
- Anything you want to place in before and after the widget (optional)
- The description – this along with the widget name will appear in the sidebar panel in the widgets section of the admin menu
- Finally anything you want to place in before and after the widget title (again optional)
Save functions.php
Now locate an area of your home.php (or even index.php if you want this new widgetized area on every listings page) and place the following php call:
1 | <?php include (TEMPLATEPATH . '/welcome-text.php'); ?> |
Save this then head on over to the admin section menu and locate widgets.
You should now see a new ‘sidebar’area called ‘welcome-text’ along with a description of ‘This is the intro text at the top of the home page’.
You can open this up and drag any widgets you want in there which will place them in the new widget area we just created.


Posted under:
