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:

WordPress Visual Cheat Sheet

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-&gt;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; ?>

Leave a Reply

Your email address will not be published. Required fields are marked *