不能让这个过于复杂的循环正确工作


Can't get this overly complicated loop to work correctly

我在我的WP主题文件夹中编辑index。php,实际上我想让它做的是:

  • 在博客首页显示4篇文章

  • 对第一页的第一篇(最新的)文章进行不同的样式。

这是我正在使用的循环,第一页看起来很好,但由于某种原因,在第2页和更高的页面上,它执行得非常奇怪,在每个额外的页面上只显示一个帖子。此外,它将只显示标题,而不显示日期或摘录。下面是我的代码:

           <?php 
            if( is_home() && !is_paged() ){
            global $query_string;
            parse_str( $query_string, $args );
            $args['posts_per_page'] = 4;
            query_posts($args);
            if (have_posts()) :
                while (have_posts()) : the_post();
           if (++$counter == 1) { ?>
            <div class="featured_post">
                <p class="date"><?php the_date('M j, Y'); ?></p>
                <a href="<?php the_permalink(); ?>"><h2><?php the_title(); ?></h2></a>
                <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('featuredblog'); ?></a>
                <?php the_excerpt(); ?>
            </div>
          <?php } else { ?>
            <div class="regular_post">
                <p class="date"><?php the_date('M j, Y'); ?></p>
                <div class="postimage"><a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('regularblog'); ?></a></div>
                <a href="<?php the_permalink(); ?>"><h3><?php
                    // short_title($after, $length)
                    echo short_title('...', 7);
                    ?></h3></a>
                <?php the_excerpt(); ?>
            </div>
          <?php } ?>
        <?php endwhile;
        else :
           // Code for no posts found here
        endif;
        } else {
            global $query_string;
            parse_str( $query_string, $args );
            $args['posts_per_page'] = 6;
            query_posts($args); ?>
            <div class="regular_post">
                <p class="date"><?php the_date('M j, Y'); ?></p>
                <div class="postimage"><a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('regularblog'); ?></a></div>
                <a href="<?php the_permalink(); ?>"><h3><?php the_title(); ?></h3></a>
                <?php the_excerpt(); ?>
            </div>
        <?php } ?>

也许有太多的如果…这里有Else语句?

您是否在使用某种框架?这是一个非常糟糕的模板引擎。

无论如何,如果你需要添加样式,你应该把这项工作留给CSS。

如果你需要非常精确的元素,你可以在一个类中为每个需要不同样式的元素留下一个计数器。

like <div class="Element<?php echo ++$counter; ?>"></div>

然后你可以在CSS中添加你的样式

.Element1,.Element2,.Element3 {styles}
.Element4 {other styles}

这样可以简化PHP代码

请记住,有一天您甚至不需要添加类计数器。在不久的将来,您可以使用nth-child css selector http://reference.sitepoint.com/css/pseudoclass-nthchild

编辑:该死的我回复了一个有50% AR的人

我的建议是遵循yes123,但实际上把它全部留给css。基本上,你所有的文章都应该被包装在父div中,并且每个都应该以相同的方式分类。

<div id="content">
    <div class="post">
        <div class="title"></div>
    </div>
    <div class="post">
        <div class="title"></div>
    </div>
    <div class="post">
        <div class="title"></div>
    </div>
    <div class="post">
        <div class="title"></div>
    </div>
</div>

这就是你应该解决的问题,显然,在文章中有更多的标签。

从那里,样式的第一个帖子不同的方式将使用#content > .post:first-child选择器。

同样,每页有不同数量的帖子可能不是一个好主意。实际上我从来没有真正尝试过,但是我可以看到分页在哪里会搞砸,并且您可能永远不会看到第5或第6个帖子。我想wordpress可能会说,每页6个帖子,第二页应该总是从第7个帖子开始。

<?php if (is_home() && !is_paged() ) {
                global $query_string;
                parse_str( $query_string, $args );
                $args['posts_per_page'] = 4;
                query_posts($args);
                } else { 
                global $query_string;
                parse_str( $query_string, $args );
                $args['posts_per_page'] = 6;
                query_posts($args);
                } ?>
            <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
            <div class="post<?php echo ++$counter; ?>">
                    <?php if ( is_home() && !is_paged() && $counter == 1 ) { ?>
                        do stuff
                    <?php } else { ?>
                        do other stuff
                    <?php } ?> 
            </div>
            <?php endwhile; ?>