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 deserve to be released, at least, in a compilation.
Divert posts to one of several single.php templates
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');
} ?>
Use header.php to call a new header for each page
Place the following code in the header.php file in the location the header should appear
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 the character length of a get_post_meta custom field
Add the following function to the functions.php file, place the additional code where the truncated custom field text should appear
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 && 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 the character length of a comment
Place the following code where truncated comments should appear
http://wordpress.org/support/topic/338430?replies=6
<?php $comments = get_comments('status=approve&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;?>
Permanently delete all comments at the same time
Use phpMyAdmin to execute the following SQL
http://wordpress.org/support/topic/312202?replies=5
DELETE FROM wp_comments WHERE comment_approved = 0
Display a users ID number once they have logged in
Place the following code in the header.php file where the ID should appear
http://wordpress.org/support/topic/232591?replies=2
<?php
global $userdata;
get_currentuserinfo();
print intval( $userdata->ID );
?>
Display a users avatar once they have logged in
Place the following code in the header.php file where the avatar should appear
http://wordpress.org/support/topic/217638?replies=6
<?php global $userdata; get_currentuserinfo(); echo get_avatar( $userdata->ID, 46 ); ?>
Create a custom bookmark icon for the iPhone
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
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 ->
Use an array to display posts from two selected categories and specify the number of posts
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(); ?>