使用自定义wordpress循环:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
//Post Content
<?php endwhile;?>
//Pagination
<?php else : ?>
//No posts message
<?php endif; ?>
我需要帮助重新构建代码,以便在每页第4页发布后显示html Div.block ,条件是页面至少要显示6个帖子。
答案 0 :(得分:1)
我在循环遍历每个结果之前定义了$postnum
,以便每次迭代都可以递增$postnum
的原始值。
<?php
$postnum = 0; // Set counter to 0 outside the loop
if (have_posts()) : while (have_posts()) : the_post();
?>
//Post Content
<?php $postnum++; // Increment counter
if ($postnum == 4){ ?>
//Div.block
<?php } ?>
<?php endwhile;?>
//Pagination
<?php else : ?>
//No posts message
<?php endif; ?>
这样,我就可以在循环内的每个页面上的第4个帖子后显示单个Div.block html。
答案 1 :(得分:0)
WordPress在The Loop中使用对象WPQuery,在对象中你有两个变量可用于确定你将在页面上显示的帖子数量。如果您将$ wp_query声明为此$wp_query->post_count;
,则变量为$wp_query->found_posts;
和$wp_query = new WP_Query( $args );
。
现在,我将在你的循环中添加一个小计数器,它将成为这个:
<?php
$size = $wp_query->post_count;
$counter = 0;
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
//Post Content
if ($size >= 6 && $counter == 3) {
//show your div here
}
$counter++;
<?php endwhile;?>
//Pagination
<?php else : ?>
//No posts message
<?php endif; ?>