如何在WordPress循环中更改帖子计数?

时间:2016-05-07 作者:user32447

嘿,我正在尝试自定义我的WordPress帖子循环,为一组特定的帖子创建不同的帖子样式。

例如,前三篇文章将以自己的方式设计风格,接下来的四篇文章将以自己的方式设计风格,等等。

此外,我想在一定数量的帖子之后增加一个休息时间。请参见下图,了解我正在努力实现的目标。

http://cdn.ratedrnb.com/2016/04/photo.png

下面的代码就是我用来改变帖子风格的代码。

<?php if (have_posts()) :
$count = 0;

// What page are we on?
$paged = ( get_query_var(\'paged\') > 1 ) ? get_query_var(\'paged\') : 1;
// How many pages of posts do we have to display?
//$max = $wp_query->max_num_pages;

while (have_posts()) : the_post();      

$count++;

if ($count  == 1 && $paged == 1) : ?> 

 <!--first post -->

 **html/style of the first post**

 <!--next four post -->

 <?php elseif ($count  > 1 && $count  <5 && $paged == 1) : ?>

 **html/style of next four post**

 <!-- 4th post and break div -->

<?php elseif ($count  == 5 && $paged == 1) : ?> 

**html/style of fourth post**

<div>This is the div break</div>

<!--next five post -->

 <?php elseif ($count >5 && $count < 11 && $paged == 1) : ?> 

**html/style of next six post**

<!--sixth post and div break-->

 <?php elseif ($count == 11 && $paged == 1) : ?>

**html/style of 6th post**

 <div>This is the div break</div>

 <!--last eight post-->

<?php elseif ($count  > 12 && $count  < 21 && || $paged == 1) : ?>

**html/style of last eight post**

  <?php endif;
endwhile;
endif; ?>
现在,这段代码的设计也使得当我转到下一页时,其余的页面采用最后八篇文章的样式。

有人能告诉我如何更改下面的这些行,以显示前3篇文章,而不是仅显示一篇。

<?php if (have_posts()) :
$count = 0;

// What page are we on?
$paged = ( get_query_var(\'paged\') > 1 ) ? get_query_var(\'paged\') : 1;
// How many pages of posts do we have to display?
//$max = $wp_query->max_num_pages;

while (have_posts()) : the_post();      

$count++;

if ($count  == 1 && $paged == 1) : ?> 
还有谁能向我解释一下,为什么我雇来做这件事的人必须像下面这样分离循环。

 <!--next four post -->

 <?php elseif ($count  > 1 && $count  <5 && $paged == 1) : ?>

 **html/style of next four post**

 <!-- 4th post and break div -->

<?php elseif ($count  == 5 && $paged == 1) : ?> 

**html/style of fourth post**

<div>This is the div break</div>
我正试图对PHP编码有更多的了解。

1 个回复
最合适的回答,由SO网友:dan9vu 整理而成

首先,转到Settings->Reading->Blog pages show at most->或更改posts_per_page 在主查询中为25。

然后,在模板中尝试以下代码:

if (have_posts()) :
    $count = 0; $paged = ( get_query_var(\'paged\') > 1 ) ? get_query_var(\'paged\') : 1;
    while (have_posts()) : the_post();
        $count++;
        if ($count <= 3 && $paged === 1) :
            if ($count === 1) echo \'<div class="break"><h2>Break div | first 3 posts</h2></div>\'; ?>
            <div class="first-three">
                <?php the_title() ?>
            </div>
        <?php elseif (3 < $count && $count <= 7 && $paged === 1) :
            if ($count === 4) echo \'<div class="break"><h2>Break div | next 4 posts</h2></div>\'; ?>
            <div class="next-four">
                <?php the_title() ?>
            </div>
        <?php elseif (7 < $count && $count <= 13 && $paged === 1) :
            if ($count === 8) echo \'<div class="break"><h2>Break div | next 6 posts</h2></div>\'; ?>
            <div class="next-six">
                <?php the_title() ?>
            </div>
        <?php elseif (13 < $count && $count <= 20 && $paged === 1) :
            if ($count === 14) echo \'<div class="break"><h2>Break div | next other 6 posts</h2></div>\'; ?>
            <div class="next-other-six">
                <?php the_title() ?>
            </div>
        <?php else :
            if ($count === 21) echo \'<div class="break"><h2>Break div | last 6 posts</h2></div>\'; ?>
            <div class="last-six">
                <?php the_title() ?>
            </div>
        <?php endif;
    endwhile; ?>
    <div class="nav-previous alignleft"><?php next_posts_link(\'Older posts\'); ?></div>
    <div class="nav-next alignright"><?php previous_posts_link(\'Newer posts\'); ?></div><?php
endif;
现在,你应该知道怎么做了。确保您至少有25篇文章要显示并注意sticky_post.