WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Post ID shown in Post Title (6 posts)

  1. Jeroen
    Member
    Posted 5 years ago #

    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>

  2. Jeroen
    Member
    Posted 4 years ago #

    Pretty please?

  3. s_ha_dum
    Member
    Posted 4 years ago #

    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.

  4. Jeroen
    Member
    Posted 4 years ago #

    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.

  5. s_ha_dum
    Member
    Posted 4 years ago #

    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.

  6. RyanRayLA
    Member
    Posted 4 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic