Here is a collection of code snippets that I have used on this site over the years. Storing them here for ease of access and to perhaps satisfy a few Google searches.

WordPress query for a post list that you can put anywhere. I’ve used it in header, footer and sidebar, as well as to have specific category listings on the post index. You can list by tag, category_name, etc and can list asc, desc, rand. You can also offset the number to skip posts.

Use normal in-loop tags that you would see on the single post.

<?php $the_query = new WP_Query(‘tag=featured&orderby=rand&showposts=1’);
while ($the_query->have_posts()) : $the_query->the_post();
$do_not_duplicate = $post->ID; ?>

Standard per-post code goes here, including:
<?php the_post_thumbnail_url( ‘full’ ); ?>
<?php the_permalink(); ?>
<?php the_title(); ?>
<?php the_time(‘F j, Y’); ?>
<?php the_category(‘, ‘) ?>

Then you close out the loop with…

<?php endwhile;
wp_reset_postdata(); ?>

Show shortcode in theme. I’ve used this quite a few times as well. It’s most useful for simple text displays that pull from a plugin.

<?php echo do_shortcode(“[the_shortcode]”); ?>

Count the total number of published posts. Outputs a simple numerical value.

<?php wpb_total_posts(); ?>

…and this needs to be added to functions.php of the same theme…

function wpb_total_posts() {
$total = wp_count_posts()->publish;
echo ‘Total Posts: ‘ . $total;
}

I also make vast use of Conditional Tags. There is a page dedicated to these on wordpress.org, but here are a few examples…

<?php if ( is_front_page() && is_home() ) { ?>
Shows on your front page.
<?php } elseif ( is_page( ‘slug_of_page’ ) ) { ?>
Shows on a specific page (remove ‘slug_of_page’ to show on any page).
<?php } elseif ( is_single( ‘slug_of_post’ ) ) { ?>
Shows on a specific post (remove slug to show on any post).
<?php } elseif ( is_category( ‘category_name’ ) ) { ?>
Shows on the category listing.
<?php } elseif ( is_tag( ‘tag_name’ ) ) { ?>
Shows on the tag listing.
<?php } elseif ( in_category( ‘category_name_attached_to_post’ ) ) { ?>
Shows on posts attached to a specific category.
<?php } elseif ( has_tag( ‘tag_name_attached_to_post’ ) ) { ?>
Shows on posts attached to a specific tag.
<?php } ?>

Of course you can easily specify a conditional NOT for any of the above like this (note the exclamation mark placement):

<?php if ( !is_page() ) { ?>
Anything here would show everywhere except a page.
<?php } ?>

Show what you have entered into a page or posts custom fields on the page or post like this:

<?php if ( get_post_meta($post->ID, ‘custom_field_name’, true) ) { ?>
<?php echo get_post_meta($post->ID, “custom_field_name”, $single = true); ?>
<?php } ?>

If you use the thumbnail feature in WordPress to attach images to your posts, you can use a conditional to show those like this:

<?php if ( has_post_thumbnail() ) { ?>
<?php the_post_thumbnail_url( ‘full’ ); ?>
<?php } else { ?>
Show another image instead here…
<?php } ?>

Show the previous or next post to the current one with their attached thumbnails included like this:

<?php
$next_post = get_next_post();
if (!empty( $next_post )): ?>

Create your post using these (note, they each are different to what you’d use for the current post):
<?php echo get_the_post_thumbnail_url($next_post->ID, ‘full’); ?>
<?php echo get_permalink( $next_post->ID ); ?>
<?php echo $next_post->post_title; ?>
<?php echo mysql2date(‘F j, Y’, $next_post->post_date, false) ?>
<?php echo get_the_category_list(‘, ‘, ”, $next_post->ID); ?>

Then close out the loop like this:

<?php endif;
wp_reset_postdata(); ?>

…change everywhere you see ‘next’ to ‘prev’ for the previous post (the code above is just for the next one).

After I am done designing a theme, I usually split it into different php files within the template directory to allow me to easier code in block that I can re-use in other areas. You include a php file in a theme like this:

<?php include (TEMPLATEPATH . ‘/menu/dropdown.php’); ?>



        

Tim is British and lives in the United States with his wife and kids.

He works for software developers Image Space Inc. and Studio 397 on their racing simulations, and is a fan of Gaming, Motorsports, and photography.

#Ad
I've served 80575 downloads and 278 posts on this site.
All content belongs to me, unless it doesn't. twhtly.net © 1999-2018