Created
March 20, 2012 16:18
-
-
Save NeilJS/2137806 to your computer and use it in GitHub Desktop.
WP - Get current page number for current (single) post inc. CPTs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* GET CURRENT PAGE NUMBER FOR THIS SINGLE POST ie. it's index number */ | |
// put in functions.php | |
// Note: edit "my-custom-post-type" or remove "AND post_type = 'my-custom-post-type'" | |
function current_post_num($display=true) { | |
global $post, $wpdb; | |
//$rows = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_date ASC"); | |
$SQL = $wpdb->get_col("SELECT ID FROM $wpdb->posts posts WHERE posts.post_status = 'publish' AND post_type = 'my-custom-post-type' ORDER BY post_date ASC"); | |
$SQL = array_flip($SQL); | |
$postnum = $SQL[$post->ID]; | |
$postnum++; | |
if($display) { | |
echo $postnum; | |
} | |
return $postnum; | |
} | |
// Usage | |
<?php current_post_num(); ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment