• Resolved Jeroen

    (@jeroenonstenk)


    I’d like to show the Post ID in the actual post title. So, for instance, post ID 3 could be shown in the title like this: “#3 This is the third post”.

    Which piece of code do I have to add before

    <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter Jeroen

    (@jeroenonstenk)

    Pretty please?

    Take a look at the the_title() function. Notice the ‘$before’ and ‘$after’ variables. Those behave just as their names suggest. Something like
    the_title('#'.$post->ID.' '); should give you what you want.

    Thread Starter Jeroen

    (@jeroenonstenk)

    Thanks a lot! That did the trick exactly!

    The only downside is that uses the actual post ID and not the number of published post. So posts I deleted are counted as well.

    Nevertheless, thanks for your help.

    Yes, that will show the Post ID. To get a post’s place in the set of published posts I’m pretty sure you’ll have to cook up a custom MySQL query. It shouldn’t be too hard if you want to give it shot.

    ryanrayla

    (@ryanrayla)

    Found this — hope it helps

    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!!!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Post ID shown in Post Title’ is closed to new replies.