最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

functions - How to get the index number of the posts?

programmeradmin3浏览0评论

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
  • 2 If you want to know the number of posts returned by WP_Query, use $manset_posts->found_posts. See the codex for more info: developer.wordpress/reference/classes/wp_query. – shanebp Commented Jun 14, 2020 at 19:45
  • thanks @shanebp but I did that part (number of posts in a specific category), I want to get the post index number. – Ugur Terzi Commented Jun 15, 2020 at 4:51
Add a comment  | 

1 Answer 1

Reset to default 0

So 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
     }
}
发布评论

评论列表(0)

  1. 暂无评论