Useful WordPress Snippets
A short list of some 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:
Free Complete WordPress Cheat Sheet
Body Class
Replace the <body> tag with
<body <?php body_class(); ?>>
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.
<?php echo get_post_meta($post->ID, 'custom-field-name', true); ?>
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.
<?php
$key = 'custom-field-name';
$themeta = get_post_meta($post->ID, $key, TRUE);
if ($themeta == 'London'){
echo 'capital of England';
} else if ($themeta == 'Edinburgh') {
echo 'capital of Scotland';
} else {
echo 'No capitals';
}
?>
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.
<?php if (has_tag()) : ?>
<div><?php the_tags('Tagged with: ',' / ','<br />'); ?></div>
<?php else : ?>
<div>Sorry, no tags here!</div>
<?php endif; ?>
Another example is that if you want to continually add elseif statements:
<?php
if (is_page('about')) {
get_sidebar('about');
} elseif (is_page('what-we-do')) {
get_sidebar('blog');
} else {
get_sidebar('page');
}
?>
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”.
<?php $customfield = get_post_meta($post->ID, 'custom-field-name', true);
if ($customfield) {
?>
<?php echo '<div class="my-class">'.$customfield.'</div>'; ?>
<?php } else { ?>
<div class="my-class">No Custom Field here</div>
<?php } ?>
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:
<?php $customfield = get_post_meta($post->ID, 'custom-field-name', true);
if ($customfield) {
?>
<?php echo '<li>'.$customfield.'</li>';
}?>
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:
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
and add the following code directly above:
<?php $posts=query_posts($query_string . '&posts_per_page=-1'); ?>
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
<?php query_posts(array('post__in'=>get_option('sticky_posts'))); ?>
Execute shortcode inside custom field
Allows shortcode to be used inside a custom field
<?php echo apply_filters('the_content', get_post_meta($post->ID, 'custom-field-name', true)); ?>
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
remove_action('wp_head', 'wp_generator');
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.
<?php if ( get_post_meta($post->ID, 'custom-field-name', true) ) : ?> <div> <?php get_post_meta($post->ID, 'custom-field-name', true) ) : ?> </div> <?php else : ?> <?php endif; ?>
-
Free up space on your Mac
You may not know it but there’s a lot of places on your shiny lovely Mac that can store lots of rubbish that you don’t actually need. We’ll explain a few places you can look to see if there’s a mass of files that you can safely delete and free up a few valuable gigs of space.
-
iPhone to Mac Wireless Networking
Connect to your jailbroken iPhone to your Mac to add or retrieve files and folders.
-
Is your site LIVE? FREE website monitoring sites
Keep a check on whether your website is live with these free and paid for tools.
-
Disable Flash in Chrome on Mac and PC
Sometimes you need to disable Flash. Maybe to test sites that run in both Flash and HTML and therefore automatically show you to the Flash elements or maybe you just don’t want Flash running in your browser. Whatever it is then here’s how to disable it in Chrome on a Mac.
-
Add category description if it exists
Find out how to add the category description to your category template in Wordpress.
-
iPhone Jailbreak for OS 3.0 Released
How to jailbreak your iPhone.
-
Backing up your Website using cPanel
Find out how to back up your website via cPanel
-
Domain Extensions. Are they important?
Does it make any difference what domain extension you choose? Find out more.
-
Web Browsing Tips #2: Tabbed Windows
Using tabbed windows in your web browser.
-
What's in a name? Choosing the right name for your company and website
Find out how to choose a great company name with a few basic tips.
-
Web Browsing Tips #3: Keyboard Shortcuts
Some web browsing keyboard shortcuts.
-
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.