WordPress: Tips, Hacks and Extras

WordPress gives us so many options that it truly is amazing they have yet to declare themselves a content management system. Sometimes, though, we have to give the program a little nudge to squeeze out every last drop of goodness and make it do what we need. The list of short tips, hacks, and extras below has been compiled over the course of several months, with no specific intent to be released in this manner, though it seemed appropriate. These "mini-tutorials" are much too short to be converted into articles but at the same time deserved to be released, at least, in a compilation.

 

Using Multiple single.php Templates

Objective:

Divert posts to one of several single.php templates

Setup:

Remove the contents of single.php and insert the code provided below. Create new single.php templates with unique names.

<?php
 if (in_category(1)) { // If the post is in category 1 redirect to the single-1.php template
     include(TEMPLATEPATH . '/single1.php');
 } elseif (in_category(2)) { // If the post is in category 2 redirect to the single-2.php template
     include(TEMPLATEPATH . '/single2.php');
 } else { // If the post is in any other category redirect to the single-3.php default template
     include(TEMPLATEPATH . '/single3.php');
 } ?>

 

Different Header For Each Page

Objective:

Use header.php to call a new header for each page

Setup:

Place the following code in the header.php file in the location the header should appear

Source:

http://chasesagum.com/different-header-on-each-page-for-wordpress

<?php if (is_front_page()) { ?>
  <div id=”header”> <!– The header id that you want on your front page –>
 <?php } else { ?>
  <div id="<?php echo $post->post_name; ?>"> <!– The alternative header, based on the page title –>
<?php } ?>

 

Limit get_post_meta In Custom Field

Objective:

Limit the character length of a get_post_meta custom field

Setup:

Add the following function to the functions.php file, place the additional code where the truncated custom field text should appear

Source:

http://wordpress.org/support/topic/231945?replies=2

function the_title2($before = '', $after = '', $echo = true, $length = false) {
  global $post;
  $title = get_post_meta($post->ID, 'key_of_your_custom_field',true);
 if ( $length &amp;&amp; is_numeric($length) ) {
  $title = substr( $title, 0, $length );
 } if ( strlen($title)> 0 ) {
  $title = apply_filters('the_title2', $before . $title . $after, $before, $after);
 if ( $echo )
  echo $title;
 else return $title;
 } }
<?php the_title2('', '...', true, '40') ?> <!- Where 40 is the character length ->

 

Limit Comment Length

Objective:

Limit the character length of a comment

Setup:

Place the following code where truncated comments should appear

Source:

http://wordpress.org/support/topic/338430?replies=6

<?php $comments = get_comments('status=approve&amp;number=5'); foreach($comments as $comm) :?>
 <?php $length = 40; // Character length
       $thiscomment = $comm->comment_content;
      if ( strlen($thiscomment) > $length ) {
       $thiscomment = substr($thiscomment,0,$length);
       $thiscomment = $thiscomment .' ...';
      } ?>

 <a href="<?php echo get_permalink($comm->comment_post_ID);?>#comment-<?php comment_ID() ?>"><?php echo ($thiscomment);?></a><br />
<?php endforeach;?>

 

Delete All Comments At Once

Objective:

Permanently delete all comments at the same time

Setup:

Use phpMyAdmin to execute the following SQL

Source:

http://wordpress.org/support/topic/312202?replies=5

DELETE FROM wp_comments WHERE comment_approved = 0

 

Display User ID When Logged In

Objective:

Display a users ID number once they have logged in

Setup:

Place the following code in the header.php file where the ID should appear

Source:

http://wordpress.org/support/topic/232591?replies=2

<?php
 global $userdata;
 get_currentuserinfo();
 print intval( $userdata->ID );
?>

 

Display User Avatar When Logged In

Objective:

Display a users avatar once they have logged in

Setup:

Place the following code in the header.php file where the avatar should appear

Source:

http://wordpress.org/support/topic/217638?replies=6

<?php global $userdata; get_currentuserinfo(); echo get_avatar( $userdata->ID, 46 ); ?>

 

Create A Custom iPhone Icon For Your Website

Objective:

Create a custom bookmark icon for the iPhone

Setup:

Design an icon measuring 57 x 57 pixels, upload the file to your site and place the following code in the header.php file above the opening body tag

Source:

http://buildinternet.com/2009/12/give-your-website-a-custom-iphone-bookmark-icon/

<link rel="apple-touch-icon" href="/location/of/icon.png" /> <!- glossy icon ->
<link rel="apple-touch-icon-precomposed" href="/location/of/icon.png" /> <!- non-glossy icon ->

 

Display Posts From Two Categories And Specify Number Of Posts

Objective:

Use an array to display posts from two selected categories and specify the number of posts

Setup:

Use the following code as the begining of a Loop

<?php
 $temp = $wp_query;
 $wp_query= null;
 $wp_query = new WP_Query();
 $wp_query->query(array('category__and'=>array(16,23), 'showposts'=>5, 'paged'=>$paged)); ?>
 <!- categories are 16 and 23 with a total of 5 posts displayed ->

 <?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
Dale Crum

Dale Crum

Owner / Creative Director at Doc4 Design