Increment, continuous post ID (4 posts)

  1. Tunghsiao Liu
    Posted 5 years ago #

    Hi all, I need to add an increment, continuous post ID to every post other than WordPress default post_ID, so do a search and found this post is useful:


    With the code <div id="post<?php echo($wp_query->current_post + 1); ?>"> I could get IDs like 1, 2, 3, 4, 5. but the next page (/page/2/) the ID of the first post reset to 1, I expect the number could be 6, 7, 8, 9 and 10. does anyone know how to make the ID continuous even in next page?

  2. Michael
    Forum Moderator
    Posted 5 years ago #

    maybe this can help:
    also read through the comments for how to reverse the numbering with the latest post starting with number 1

  3. Tunghsiao Liu
    Posted 5 years ago #

    Thanks a lot, but this only works in normal post query. I mean, if it's in a posts list of specified tag or category, the numbers are still messed up.

  4. son0fhobs
    Posted 5 years ago #

    As apposed to going through the Post ID's, why don't you collect all the posts in an array, then go through the array? Or does it require a query, thus requiring the actual ID stored in the database?

    If not...

    Reference this:

    I had trouble doing it without messing up the original loop (although there are better fixes)
    Here's mine:

    $newposts = get_posts('numberposts=-1&category=1');
     	//get all posts (no limit) in new category (look up function in codex for full explination
    $maxnew = count($newposts);	// count # posts in array
    for($q=0;$q<$maxnew;$q++){    // Loop through posts array
    	$newpostsid[$q] = $newposts[$q]->ID;
                              // assign post ID's to new array
    	echo $newpostsid[$q] . " ";
    //Just double check that it increments all the way through, the # of posts there are.

    $newpostsid[$q] will be the current post for each loop.

    Good luck!

Topic Closed

This topic has been closed to new replies.

About this Topic