RSS

Make an Awesome functions.php File For You New WordPress Template – Part 1

Its been awhile since I threw up a new blog post. Been working hard and trying to finish up some grad-school applications. However I have been in the midst of creating a new WordPress theme and have discovered that a really great functions.php file can be an enormous help for the template designer AND for whoever may need to edit your template later. If you are considering creating a template that you will eventually distribute or if you know that someone besides you will eventually have to modify the template you should definitely put a lot of thought into your functions.php file. In this post I am going to put some generic and useful functions and shortcode included that you can use if you like. Note: It is good practice to put a prefix before your functions. You don’t want use the same function names as WordPress or other plugins, because this will cause an error. You can create a pseudo-namespace for you functions by prepending them with some generic prefix. Here I am using AM.

Easily Grab a Custom Field

WordPress is not a CMS, however many developers are using the built in functionality of WordPress to mimic some of the features of a CMS. One of the most helpful features are Custom Fields. These will allow you to associate unique data with each page or post. This function is a nice wrapper for the built in one for grabbing a custom field:

function AM_get_custom_field_value($key, $post_id = null, $echo = false) {  
  global $post;  
  $post_id = ($post_id)?($post_id):($post->ID);
  $value = get_post_meta($post_id, $key, true);  
  if($echo){
    echo $value;
  }
  return $value;
}

Note: this function will only grab a single value for your custom field. In order to grab the whole array, here is a different function:

function AM_get_custom_field_values($key, $post_id = null) {  
  global $post;  
  $post_id = ($post_id)?($post_id):($post->ID);
  return get_post_meta($post_id, $key, false); 
}

Insert a Single, Specific Post Into Your Template

Often times clients want to be able to edit certain parts of a webpage without editing the template code. With most modern CMSes this is handled using modules or something similar. Although WordPress has Widgets, one big downside of using them is that the text widget doesn’t have the same nice WYSIWYG that a post or page does. One way around this is to create specific posts that contain editable blocks of text and display them directly in your template. Now, this isn’t exactly the “proper” use of posts, but who cares, it gets the job done. One thing to remember though, if you don’t want this text showing up in your blog lists, you must make sure NOT to publish it. The other thing is that you will need to get the Post ID for the post you want to display. This is available in the URL while editing the post:

wp-admin/post.php?action=edit&post=541&message=1

In this case the Post ID is 541.

function AM_get_single_post($postID, $echo = false){
  ob_start();  
  query_posts('p='.$postID);
  if ( have_posts() ) : while ( have_posts() ) : the_post();
    the_content();
  endwhile; 
  endif;
  //Reset Query
  wp_reset_query();
  $post_content= ob_get_clean();
  if($echo){
    echo $post_content;
  }
  return $post_content;
}

Insert a Post Inside a Post

This might seem like a useless function, but I have actually used it quite successfully. It goes along the same lines as the above function but will allow you to insert a post inside another post using shortcode. I recently used this for a client who wanted an editable block of text, but wanted to put it on only certain posts. It didn’t seem to necessitate a whole different template file for this, so I decided to write this function. In order to use it, you need to add this shortcode inside your post:

[insert_the_post id="88"]
function insert_the_post_func($atts) {
	extract(shortcode_atts(array(
		'id' => '0',
	), $atts));
  if($id){
    query_posts('p='.$id);
    //The Loop
    if ( have_posts() ) : while ( have_posts() ) : the_post();
      $content = get_the_content();
    endwhile; 
    endif;
    //Reset Query
    wp_reset_query();
  }
	return $content;
}
add_shortcode('insert_the_post', 'insert_the_post_func');

Just testing

  • Share/Bookmark
 No Comments yet, your thoughts are welcome!

Leave a Reply