Useful WordPress Snippets

There’s lots and lots of things to remember when using WordPress and you can’t remember everything. Here’s a list of useful little snippets that I’ll keep on adding to over time. To be honest it’s more for my record as it’s stuff that I use regularly but thought I’d add for all to share.

WordPress Cheat sheets

Not so much cheating as great reference guides. There are lots and lots of useful WordPress tags that you can add to pages and templates. Here are a couple of great WordPress Cheat Sheets:

WordPress Visual Cheat Sheet

Free Complete WordPress Cheat Sheet

Body Class

Replace the <body> tag with

This will allow you to change elements on the page on a page-by-page basis simply by referencing the body in your CSS. For example you may see this, when you view the source code of your live page:

<body class=”page page-id-2 page-template page-template-default logged-in”>

which would then allow you to target elements on that specific page in your CSS.

For more information visit the Official WordPress page.

Custom field in WordPress template

Adds a field within your template that will show custom bits of text, images etc that you add from within the post/page admin. Add the code to your template e.g.

Then add a new custom field to your page/post and add the name e.g. custom-field-name.

Custom field in WordPress template with if else statement

There are situations where you need to see whether the custom field has content of a certain criteria and then display something else.

In this example the “custom-field-name” is the name of the custom field you are calling. If the content of that custom field contains “London” then the outputted text will show as “capital of England”. If “Edinburgh” then the content will be “capital of Scotland”. If no content exists in that custom field then the text “No capitals” will be displayed.

If Else WordPress

The following code will add tags to the post, if there are any, if there’s not then it will add a bit of text. This function can be used in lots of different ways and is incredibly useful.

Another example is that if you want to continually add elseif statements:

Wrap your Custom Field in DIV tags

It’s straight forward to show your custom field using the code mentioned above but what it would like to wrap that custom field in DIV tags or Paragraph tags or whatever else you fancy. Here’s a few options:

Display and wrap the custom field named “custom-field-name” in the div class “my-class”. If there’s no Custom Field by that name or nothing is entered in that field then display the text “No Custom Field here”.

The following will only show the Custom field if it exists. It will also be wrapped in list tags. If not then nothing will be shown:

Conditional tags

Useful tags for use in your WordPress templates: http://codex.wordpress.org/Conditional_Tags

Control the amount of posts per page/category/search or archive

Open the WordPress template file and locate the following:

and add the following code directly above:

The -1 indicates an infinite amount of posts – you can change this to whatever amount you’d like to appear. Bear in mind the page load time and file size if using an infinite amount.

Get all Sticky Posts

Insert this code into your template file to display all of your Sticky Posts

Execute shortcode inside custom field

Allows shortcode to be used inside a custom field

Remove WordPress Generator Tag

Removes the WordPress Tag from your source code. Good for security. Place this in your theme’s functions.php file just above the ?> code

Another Custom Field Statement

This will check to see if there is any content in the specified custom field. If there is then it’ll display the content from that custom field. If not, then it won’t.

  • wordpress

    WordPress Plugins we use

    Each WP project varies and so that has a knock on effect on what plugins we use so this is by no means a definitive list, but merely our favourites that get used quite a lot

  • wordpress-login-logout-link

    10 Useful WordPress Admin plugins

    There’s a lot of plugins available for Wordpress and you often miss out on some of the gems, purely because there’s so many to trawl through and you might not know what you’re after until you stumble upon it. Here we’re focussing on those plugins that can help with the behind the scenes part of Wordpress – the CMS Admin.

  • wordpress-login-logout-link

    WordPress Login / Logout Link in Template

    The standard Wordpress Login/Logout links often suffice but when you are building custom pages then the default Wordpress pages can look a little out of place on your site and therefore you may need to redirect your users to different pages and/or change the text links of those standard links.

  • wordpress-login-logout-link

    Exclude current post from WP Query

    There are times when you don’t want the current post/page you are viewing to show on the page i.e. list of recent posts, more posts from the same category and so on. Here’s how to remove that post from your query.

  • dare-to-think-square

    Show and hide text in a form field using onclick and onblur

    A great way to reduce the need for a field heading/title is to include the informative text within the text field. This is most commonly used in search fields, where there’s often no further available room to include a heading alongside or above.So from having a black search field like this: You can have one […]