Thread Starter
hoktar
(@hoktar)
Well, no that’s exactly what I can unfortunately not use because of what I described above 😉
See, let’s say I have 2 posts, but each post was edited 3 times, now in the DB it looks like this:
ID | post_title | post_status
—————————–
1 | bla | publish
2 | bla | refurbish
3 | bla | refurbish
4 | lorem | publish
5 | lorem| refurbish
6 | lorem| refurbish
You see, my two posts have the IDs 1 and 4 because of those backups hat use the same IDs, while i would need them to have he IDs 1 and 2.
I am not sure whether there are any functions for that.
But simple php code like the following will work.
Put <?php $i=1; ?> before the loop in index.php.
Inside the loop, put
<?php
$i=$i+1;
echo $i; ?>
ahh you want sequential numbering not related to actual post ids. My apologies, Gisha is correct for listing purposes within the loop.
But I’m not sure that is what you want either, for the number to display on individual posts it’ll need something a little more complex.
here is something i prepared earlier …
Thread Starter
hoktar
(@hoktar)
First of all thank you guys for all your answers!
@gisha James
That’s exactly what I use in the loop 🙂 but when it comes to displaying the numer of only one post Rich ‘elfin’ Pedley is right, I need something different. But thanks!
@alchymyth
Once again, you are my hero!
I’ll try that right!
Thread Starter
hoktar
(@hoktar)
Unfortunately it doesn’t work as hoped.
There is no output from the function.
I put this into my fuctions.php in my theme
function updateNumbers() {
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' );
]
And this is my code for displaying the post:
<?php
global $post;
$myposts = get_posts('numberposts=1&offset=0&category=3');
foreach($myposts as $post) : setup_postdata($post);?>
//then some output like the_content()...
//and herer the call to get the number: but this doesn's show anything:
<?php echo get_post_meta($post->ID,'incr_number',true); ?>
<?php endforeach; ?>
Thread Starter
hoktar
(@hoktar)
Ok now THIS really creapes me out, all of a sudden it works. I changed absolutely nothing, just reloaded half an our later and it worked.
You might say, cache but I also cleared the cache before and it didn’t help. But now it works. No Idea why, but who cares 😀
Thanks a lot all of you and especially alchymyth!
I changed absolutely nothing, just reloaded half an our later and it worked.
as is mentioned in my post, the function is only triggerd when you edit/delete/create a post.
when you add the function to the functions.php, until you do some of the above, the custom fields will not get generatd and filled; same with post previews – the field will be empty until you save the post.
a bit confusing, i admit.
Thread Starter
hoktar
(@hoktar)
Ahh right, I forgot bout this! Thanks again!