• I’m wondering if it’s possible to show the post number of the post?
    E.g. the third post I write should say “Post no. 3”.. And so on..

Viewing 14 replies - 1 through 14 (of 14 total)
  • Every post written in WP has a post ID # that identifies it, in the database.

    You can add that ID # to your post by using <?php the_ID(); ?> ,
    just as you will find in your theme <?php the_content(); ?>

    Just add that to your template files.

    More info:
    http://codex.wordpress.org/Template_Tags/the_ID

    Thread Starter conditionkill

    (@conditionkill)

    Yeah.. Uhm.. Already tried that.. Didn’t really work..
    The ID’s for the posts increases by 2 for each post (sometimes (randomly) even more).. : \
    Here’s a screen of how it looks with the id tag:
    http://img6.imageshack.us/img6/97/200902201254312sz2.png

    How are you defining “third post I write” then – do you mean the 3rd post that is displaying on the page the person is looking at?

    Thread Starter conditionkill

    (@conditionkill)

    Uhm.. No.. The 3rd post ever written..

    WordPress doesn’t keep track of things that way. What happens to the # sequence if you delete a post you wrote 6 months ago? Or change it from published to private or draft status? What happens if you change the published date of a post you wrote 2 weeks ago to be 2 weeks previous, or two weeks ahead of its first published date?

    So the order would need to be assign in a dynamic query run every time you want to find out a post’s #. I assume you want the oldest to start at #1, which means to know the # of a post written today, every post you have ever written would need to be included in the query, to determine what the present post # should be.

    This can be done, but its a lot of work.

    On the other hand, someone else may come up with a better approach than I have.

    Thread Starter conditionkill

    (@conditionkill)

    Hmm.. Okay.

    function Get_Post_Number($postID){
    	$postNumberQuery = new WP_Query('orderby=date&posts_per_page=-1');
    	$counter = 1;
    	$postCount = 0;
    	while ($postNumberQuery->have_posts()) : $postNumberQuery->the_post();
    		if ($postID == get_the_ID()){
    			$postCount = $counter;
    			echo("true");
    		} else {
    			$counter++;
    		}
    	endwhile;
    	return $postCount;
    }

    I was looking for solutions for something similar but there isn’t a lot around in the way of actual answers…
    This is just an idea to get started but unfortunately because it’s a second loop you can’t run it inside the main loop without it interfering (it overwrites the_post() global variable I’m guessing). I’ll be looking into running a loop inside a loop soon enough though.

    Update on that:

    function Get_Post_Number($postID){
    	$temp_query = $wp_query;
    	$postNumberQuery = new WP_Query('orderby=date&posts_per_page=-1');
    	$counter = 1;
    	$postCount = 0;
    	if($postNumberQuery->have_posts()) :
    		while ($postNumberQuery->have_posts()) : $postNumberQuery->the_post();
    			if ($postID == get_the_ID()){
    				$postCount = $counter;
    			} else {
    				$counter++;
    			}
    	endwhile; endif;
    	wp_reset_query();
    	$wp_query = $temp_query;
    	return $postCount;
    }

    will work inside your main loop:

    <?php if (have_posts()) : ?>
    	<?php while (have_posts()) : the_post();
    		$currentID = get_the_ID();
    		$currentNumber = Get_Post_Number($currentID);

    etc. etc.
    Also I’m deliberately ordering them so that the newest post is number 1 but I don’t think it would take much more to order them oldest first if that is what you wanted.

    Fascinating solution. How would you insert

    $currentID = get_the_ID();
    $currentNumber = Get_Post_Number($currentID);

    inside a div, though?

    Thanks a MILLION SRJM!!

    Let me repeat how this works and elaborate and also explain how to change the assigned post number from oldest last/newest first.

    Create a new function in the Functions.php as follows

    function Get_Post_Number($postID){
    	$temp_query = $wp_query;
    	$postNumberQuery = new WP_Query('orderby=date&order=<strong>DESC</strong>&posts_per_page=-1');
    	$counter = 1;
    	$postCount = 0;
    	if($postNumberQuery->have_posts()) :
    		while ($postNumberQuery->have_posts()) : $postNumberQuery->the_post();
    			if ($postID == get_the_ID()){
    				$postCount = $counter;
    			} else {
    				$counter++;
    			}
    	endwhile; endif;
    	wp_reset_query();
    	$wp_query = $temp_query;
    	return $postCount;
    }

    (Change above bolded ASC to DESC if you want to see the numbers in reverse (latest post as highest number).)

    Then in the index.php (or page template), anywhere after “<?php if (have_posts()) : while (have_posts()) : the_post(); ?>” just add the following code wherever you want the Post # to show up.

    <?php $currentID = get_the_ID(); ?>
    <?php $currentNumber = Get_Post_Number($currentID); ?>
    
    <?php echo $currentNumber; ?>

    Good luck, everyone!!!

    Thanks! This works great BUT the ASC to DESC solution doesn’t seem to work. Anyone else has any suggestions on how one could see the numbers in reverse (latest posts as highest number)? Thanks!

    Another problem. Since implementing this solution posts have different titles, but all of the have the same body — that is the wordpress is showing only one post throughout the blog. Any ideas on why this might be happening?

    Hi, thanks for the wonderful solution. I got the desc to asc to work by just removing the strong tags, perhaps this was your problem as well videmarko?

    As videmarko said the code seems to be buggy, except for me the titles are all the same and the body… The numbers seem to work though.

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘Show post number’ is closed to new replies.