I was trying to get the index number of the posts which belong to a specific category. I tried to create a shortcode to achieve that but no luck, it returns "1" for all posts.
// Get the current post index number by [manset_post_index]
function manset_post_index() {
$manset_posts = new WP_Query( array(
'post_type' => 'post',
'post_status' => 'publish',
'category_name' => 'manset',
'update_post_term_cache' => false,
'update_post_meta_cache' => false,
'cache_results' => false,
) );
if ( $manset_posts->have_posts() ) : $manset_posts->the_post();
$thenumber = $manset_posts->current_post + 1;
endif;
return $thenumber;
}
add_shortcode( 'manset_post_index', 'manset_post_index' );
Any help would be appreciated.
Note: I'm using this shortcode [mycategory_post_index] in a layer of Post Based Revslider. The frontend result should be like "3" which means: the 3rd (3rd post in descending order) post in that specific category.
The solution over Revslider API:
By the way, there is another solution based on Revslider API, someone might need it, so I am sharing for good. This goes to rev slider custom js console.
var api = revapi6,
numberText;
api.one('revolution.slide.onloaded', function() {
numberText = api.find('.slide-status-numbers').text('1');
api.on('revolution.slide.onbeforeswap', function(e, data) {
numberText.text((data.nextslide.index() + 1));
});
});
then you add "slide-status-numbers" class name into your layer attributes.
I was trying to get the index number of the posts which belong to a specific category. I tried to create a shortcode to achieve that but no luck, it returns "1" for all posts.
// Get the current post index number by [manset_post_index]
function manset_post_index() {
$manset_posts = new WP_Query( array(
'post_type' => 'post',
'post_status' => 'publish',
'category_name' => 'manset',
'update_post_term_cache' => false,
'update_post_meta_cache' => false,
'cache_results' => false,
) );
if ( $manset_posts->have_posts() ) : $manset_posts->the_post();
$thenumber = $manset_posts->current_post + 1;
endif;
return $thenumber;
}
add_shortcode( 'manset_post_index', 'manset_post_index' );
Any help would be appreciated.
Note: I'm using this shortcode [mycategory_post_index] in a layer of Post Based Revslider. The frontend result should be like "3" which means: the 3rd (3rd post in descending order) post in that specific category.
The solution over Revslider API:
By the way, there is another solution based on Revslider API, someone might need it, so I am sharing for good. This goes to rev slider custom js console.
var api = revapi6,
numberText;
api.one('revolution.slide.onloaded', function() {
numberText = api.find('.slide-status-numbers').text('1');
api.on('revolution.slide.onbeforeswap', function(e, data) {
numberText.text((data.nextslide.index() + 1));
});
});
then you add "slide-status-numbers" class name into your layer attributes.
Share Improve this question edited Jun 14, 2020 at 19:22 Ugur Terzi asked Jun 14, 2020 at 18:13 Ugur TerziUgur Terzi 357 bronze badges 2 |1 Answer
Reset to default 0So you want the index numbers for the posts array returned by WP_Query ?
Try this:
if ( $manset_posts->have_posts() ) {
foreach( $manset_posts->posts as $key => $value ) {
// $key is the index number for each post
}
}
$manset_posts->found_posts
. See the codex for more info: developer.wordpress/reference/classes/wp_query. – shanebp Commented Jun 14, 2020 at 19:45