Wordpress循环:获取循环内的当前帖子计数

Kar*_*Rao 7 php wordpress loops

在The Loop中,我想检索当前的帖子数.

例如,在每3个帖子之后,我想插入一个广告.

那么,我如何获得循环计数的值?

The*_*dic 19

您可以使用对象实例的current_post成员WP_Query来获取当前的后迭代;

while ( have_posts() ) : the_post();

    // your normal post code

    if ( ( $wp_query->current_post + 1 ) % 3 === 0 ) {

        // your ad code here

    }

endwhile;
Run Code Online (Sandbox Code Playgroud)

请注意,如果您在函数中使用它,则需要全局化$wp_query.