Continuous post numbers for your blog

post_numbersif you look at my blog, you may notice that all posts are numbered with a unique number – starting with 1 for the oldest post and increasing continuous for each published post.

how is it done?

with a function that is hooked to some wordpress core actions;

the function takes care of everything:

  • gets a list of all published posts
  • counts through the posts
  • adds or updates the custom field
function updateNumbers() {
/* numbering the published posts: preparation: create an array with the ID in sequence of publication date, /
/ save the number in custom field 'incr_number' of post with ID /
/ to show in post (within the loop) use <?php echo get_post_meta($post->ID,'incr_number',true); ?>
/ alchymyth 2010 */
global $wpdb;
$querystr = "SELECT $wpdb->posts.* FROM $wpdb->posts WHERE $wpdb->posts.post_status = 'publish' AND $wpdb->posts.post_type = 'post' ";
$pageposts = $wpdb->get_results($querystr, OBJECT);
$counts = 0 ;
if ($pageposts):
foreach ($pageposts as $post):
setup_postdata($post);
$counts++;
add_post_meta($post->ID, 'incr_number', $counts, true);
update_post_meta($post->ID, 'incr_number', $counts);
endforeach;
endif;
}
add_action ( 'publish_post', 'updateNumbers' );
add_action ( 'deleted_post', 'updateNumbers' );
add_action ( 'edit_post', 'updateNumbers' );

save it to functions.php of your wordpress theme.

anywhere you want to display this post number, use the following line:

<?php echo get_post_meta($post->ID,'incr_number',true); ?>

just be aware that these numbers are not unique numbers ‘for life’ as they will be reallocated when you delete a post, or when you change the post order by changing the publish date.

Remark:
to trigger the function to generate the custom fields and fill them with the numbers, you need at least to make a new post, edit one post, or delete one post.

PS: the same thing starting with number 1 for the latest post –
different custom field key, hidden.

Different header image for each page in wordpress

how to show a different image for each page, for instance in the header?

simple, all you have to do with this method is to create an image file with the page ID as part of the image name.

the code for this is really short:

<?php $page_id=get_the_ID();
if(is_page()) { $image='head-image-'.$page_id.'.jpg'; };
if(!file_exists(TEMPLATEPATH.'/images/'.$image)) { $image='head-image.jpg'; }
echo '<img src="'.get_bloginfo('template_url').'/images/'.$image.'" alt="" />'; ?>

this is all.

edit:
if you are using a child theme, and are storing the images in the child theme’s /images folder, replace any ‘template’ references with ‘stylesheet’ – for example:

<?php if(is_page()) { 
  $page_id=get_the_ID(); $image='head-image-'.$page_id.'.jpg'; 
  if(!file_exists(STYLESHEETPATH.'/images/'.$image)) { $image='head-image.jpg'; }
  echo '<img src="'.get_stylesheet_directory_uri().'/images/'.$image.'" alt="" />'; 
}; ?>

end of edit

first step: create an image file name including the page ID (on pages only);
second step: check if a matching image file exists in the images folder of the theme;
third step: if yes, echo the image name; otherwise echo a default image name.

edited and extended:
if the header image is a css background image, this code can be easily adapted:
put it into an embedded style declaration into the <head> section of header.php:

<?php $page_id=$post->ID;
if(is_page()) { $image='head-image-'.$page_id.'.jpg'; 
if(!file_exists(TEMPLATEPATH.'/images/'.$image)) { $image='head-image.jpg'; }
echo '#header { background: url(' . get_bloginfo('stylesheet_directory') . '/images/'.$image.') center top no-repeat; }'; 
}; ?>

this can even be changed to be used to show different header images for each post …

Troubleshooting wordpress themes

here is a little trick if you need to check some variables, or try something new in your wordpress theme while the site is live.
use a conditional if statement to check that you are logged in as the original administrator, and only then do what you need to do. this way you can perform and display the neccesary tests, without the public seing anything.

<?php
if ('1' == $user_ID) :
echo 'you are logged in - this will not be shown to the public: <br/><br/>';
  // test routine begin //
echo 'cat-id , cat-slug , cat-name , number of posts <br/><br/>';
$categories= get_categories('');
foreach ($categories as $cat) {
echo $cat->cat_ID.' , '.$cat->category_nicename.' , '.$cat->cat_name.' , '.$cat->category_count.'<br/>';
}
  // test routine ends //
echo '<br/>end of logged in section <br/>';
endif; ?>

in this example, i displayed a full list of all categories with id, slug, and name.

the wordpress back-end (dashboard) is generally a good place to work, however for developers, some lists don’t show all the variables or parameters that would be useful for programming custom displays or functions. for instance, the IDs of posts, pages, categories, tags, etc. are not easily found in the back-end.

you can (and sometimes need to) build your own code snippets to show them on the spot where you are working with them.
this avoids a lot of guesswork.

and now you know how to hide these displays from public view.