Useful WordPress theme snippets

Posted on 7th June, 2013 Leave a Comment

This is an ever growing, evolving list of useful WordPress snippets, created as a reference for theme developers and hobbyists.
I suggest you drag this link to your favorites and bookmark this page, then simply copy & paste each snip as it’s needed then add it to your theme file. If you’d like to add to the list then add them to your comments below.

Header Snippets

<?php bloginfo('name');?> #Site Title

<?php wp_title();?> #Page or post title

<?php bloginfo('stylesheet_url');?> #stylesheet url

<?php bloginfo('pingback_url');?> #pingback url of your site

<?php bloginfo('template_url');?> #Location of the site template file

<?php bloginfo('version');?> #WordPress version

<?php bloginfo('atom_url');?> #Atom url for the site

<?php bloginfo('rss2_url');?> #RSS url for the site

<?php bloginfo('html_type');?> #Html version of the site

<?php bloginfo('charset');?> #Charset papameter of the site

Theme Snippets

<?php the_author();?> #Author of a specific post or page

<?php the_ID();?> #ID of a specific post or page

<?php edit_post_link();?> #Edit link for a post or page

<?php get_links_list();?> #Links from the blogroll

<?php wp_get_archives();?> #List of archives

<?php comments_template();?> #Comments file content

<?php wp_list_pages();?> #List sites pages

<?php wp_list_cats();?> #List of sites categories

<?php next_post_link('%link');?> #Link to the next post

<?php previous_post_link('%link');?> #Link to the previous post

<?php get_calendar();?> #Show the sites calendar

<?php post_nav_link();?> #Next and previous post link

<?php bloginfo('description');?> #Site Description

<?php the_content();?> #Content of the post

<?php if(have_posts());?> #Checks if there are post

<?php while(have_posts()):the_post();?> #Shows posts if they're available

<?php endwhile;?> #Ends the while

<?php endif;?> #End the if statement

<?php get_header();?> #Get the header.php content

<?php get_sidebar();?> #Get the sidebar.php content

<?php get_footer();?> #Get the footer.php cotent

<?php the_time('d-m-y');?> #Show the date - change the order to suit

<?php comments_popup_link();?> #Link for the comments on the post

<?php the_title();?> #Title of specific post or page

<?php the_permalink();?> #Url of the specific post or page

<?php the_category();?> #category of the specific post or page

Extras

/%category%/%postname%/ #Custom permalinks

<?php bloginfo('url'); ?> #Site url

<?php bloginfo('description'); ?> #Displays the site description

<?php the_post_thumbnail();?> #Displays the post thumbnail

<?php get_bloginfo('template_directory');?> #Location of the site template file

<?php include(TEMPLATE PATH HERE.'/X');?> #Include any file

<?php the_search_query();?> #Value for the search form

<next page> #DIVIDES THE CONTENT INTO PAGES

<more> #CUTS OFF THE CONTENT AND SHOWS READ MORE

<?php wp_meta();?> #Admins meta

CSS

/* Remove the phantom empty paragraph tags from your posts. */
p:empty {
	display:none;
} 

/* Make your posts images responsive */
#content img {
	max-width: 100% !important; 
	max-height: 100% !important;
} 

Leave a Comment

To preserve code added to a comment you can wrap your code in short tags
by using [square brackets]:

  1. PHP use - [php] <?php code here ?> [/php]
  2. CSS use - [css] #code-here {} [/css]
  3. HTML use - [html] <div> code here </div> [/html]
  4. JS use - [js] $(".codeHere") [/js]