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; ?>
-
HTML email signature in Apple Mail
Step by step guide on how to add an HTML signature to Apple Mail.
-
Secure your WordPress Blog
A guide to securing your WordPress website.
-
Remove the shadow from Mac Screen Grabs
How to remove the shadow from Apple Screengrabs.
-
10 Useful Sites for Web Developers
Some useful websites for web developers.
-
3 FREE Apps to protect your PC
A few free apps to help you protect your PC.
-
Web Browsing Tips #1: Find in Page
Find copy in page in a web page.
-
Web Browsing Tips #2: Tabbed Windows
Using tabbed windows in your web browser.
-
Web Browsing Tips #3: Keyboard Shortcuts
Some web browsing keyboard shortcuts.
-
Redirect your site the Search Engine Friendly way
Using a 301 redirect is important when you change your website URL or web page URL.
-
Remove the WordPress Meta Tag from your Blog
Remove the WordPress meta tag from your WordPress site with this small function.
-
Stay secure, use more than one password
We need passwords for everything, but how can we stay secure and have multiple passwords without having to remember them all.
-
Forward your emails to one address
Find out how, using cPanel, you can forward your emails to one email address.