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:

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.

Leave a Reply

Your email address will not be published.