Php Wordpress循环问题

Php Wordpress循环问题,php,html,loops,wordpress,Php,Html,Loops,Wordpress,在我的网站主页上,我希望最新的文章是最大的,然后旧的文章是较小的和低于它。见图片 我已经创建了一个wordpress循环,它部分地起到了作用,我缩小了范围,这样你可以获得更清晰的视图。 似乎每一篇旧文章都有一个章节存档,我希望所有旧文章都作为文章放在章节存档中。最好的办法是创建两个单独的循环 顶部循环只带回一个帖子! 底部循环返回其他4个循环 // The Loop while ( have_posts() ) : the_post(); echo '<li>';

在我的网站主页上,我希望最新的文章是最大的,然后旧的文章是较小的和低于它。见图片

我已经创建了一个wordpress循环,它部分地起到了作用,我缩小了范围,这样你可以获得更清晰的视图。




似乎每一篇旧文章都有一个章节存档,我希望所有旧文章都作为文章放在章节存档中。

最好的办法是创建两个单独的循环

顶部循环只带回一个帖子! 底部循环返回其他4个循环
// The Loop
while ( have_posts() ) : the_post();
    echo '<li>';
    the_title();
    echo '</li>';
endwhile;

// Reset Query
wp_reset_query();
?>    
</div>
<div class="subTop">
<?php
query_posts( 'posts_per_page=4' );

// The Loop
while ( have_posts() ) : the_post();
    echo '<li>';
    the_title();
    echo '</li>';
endwhile;

// Reset Query
wp_reset_query();
?>   
</div>
//循环
while(have_posts()):the_post();
回音“
  • ”; _title(); 回音“
  • ”; 结束时; //重置查询 wp_reset_query(); ?>
    希望这能有所帮助


    M

    您需要运行两个循环,一个用于主要帖子,另一个用于归档帖子。你不能只在中间放一个计数器,希望HTML能正确地格式化它自己。 像这样的东西可能有用

    
    

    如果我理解你的问题,我不认为你需要多个循环,而是我认为你可以在循环中使用“特殊”的情况来处理第一篇最新的文章,但然后正常地处理所有旧的文章(看起来你是想反过来做)

    这个怎么样:

      <?php 
      $firstPost = true; 
      query_posts('showposts=5');
      while (have_posts()) {
         the_post();
         if ($firstPost) {
           ?>
             <section class="latest-blog">
                my_article();
             </section><!-- /latest-blog -->
    
             <section class="archive">
    
         <?php 
           $firstPost = false;
         } // end of if(firstPost)
         ?>
    
         my_article();
    
       <?php
       } // end of the loop
       ?>
    
    </section><!-- /archive -->
    
    <?php
    function my_article() {
       ?>
       <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
          <!-- Post Title -->
            <h1><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h1>
          <!-- /Post Title -->
          <?php html5wp_excerpt('html5wp_index'); // Build your custom callback length in functions.php ?>
          <br class="clear"> 
          <?php edit_post_link(); ?>  
       </article>
       <?php
    }
    ?>
    
    
    我的文章();
    我的文章();
    
    如果从数据的角度来看,这些帖子都是相同的,那么我就没有真正的理由执行单独的查询来检索它们。只需以不同的方式呈现第一个。这样做可以减少您的代码,这意味着错误的位置更少,并减少数据库开销,这意味着一个性能更好的站点


    还要注意的是,这表明这不是一个有效的方法来做你正在做的事情。因此,一旦您让它按原样工作,您可能希望调查WP推荐的使用
    pre_get_posts
    操作的方法,尽管这在“页面”的情况下可能不适用/不合适。

    将代码分成两个循环

    特色帖子的第一个循环:

    <?php query_posts('showposts=1'); ?>
    <section class="latest-blog">
    <?php $i = 0; while (have_posts()) : the_post(); ?>
      <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
            <!-- Post Title -->
        <h1>
            <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
        </h1>
        <!-- /Post Title -->
        <?php html5wp_excerpt('html5wp_index'); // Build your custom callback length in functions.php ?>
    
            <br class="clear">
    
        <?php edit_post_link(); ?>
    
       </article>
    <?php endwhile; ?>
    </section>
    
    <?php wp_reset_query(); ?>
    <?php query_posts('showposts=5&offset=1'); ?>
    <section class="archive">
    <?php $i = 0; while (have_posts()) : the_post(); ?>
      <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
            <!-- Post Title -->
        <h1>
            <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
        </h1>
        <!-- /Post Title -->
        <?php html5wp_excerpt('html5wp_index'); // Build your custom callback length in functions.php ?>
    
            <br class="clear">
    
        <?php edit_post_link(); ?>
    
       </article>
       <?php endwhile; ?>
    </section>
    
    
    
    和其他帖子的第二个循环:

    <?php query_posts('showposts=1'); ?>
    <section class="latest-blog">
    <?php $i = 0; while (have_posts()) : the_post(); ?>
      <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
            <!-- Post Title -->
        <h1>
            <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
        </h1>
        <!-- /Post Title -->
        <?php html5wp_excerpt('html5wp_index'); // Build your custom callback length in functions.php ?>
    
            <br class="clear">
    
        <?php edit_post_link(); ?>
    
       </article>
    <?php endwhile; ?>
    </section>
    
    <?php wp_reset_query(); ?>
    <?php query_posts('showposts=5&offset=1'); ?>
    <section class="archive">
    <?php $i = 0; while (have_posts()) : the_post(); ?>
      <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
            <!-- Post Title -->
        <h1>
            <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
        </h1>
        <!-- /Post Title -->
        <?php html5wp_excerpt('html5wp_index'); // Build your custom callback length in functions.php ?>
    
            <br class="clear">
    
        <?php edit_post_link(); ?>
    
       </article>
       <?php endwhile; ?>
    </section>
    
    
    
    您会注意到,我们在查询中使用offset=1将第一篇文章从第二个循环中偏移(因此它不会出现两次)

    您的最终代码如下所示:

    <?php if (have_posts()): ?>
    <?php query_posts('showposts=1'); ?>
    <section class="latest-blog">
    <?php $i = 0; while (have_posts()) : the_post(); ?>
      <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
            <!-- Post Title -->
        <h1>
            <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
        </h1>
        <!-- /Post Title -->
        <?php html5wp_excerpt('html5wp_index'); // Build your custom callback length in functions.php ?>
    
            <br class="clear">
    
        <?php edit_post_link(); ?>
    
       </article>
    <?php endwhile; ?>
    </section>
    
    <?php wp_reset_query(); ?>
    <?php query_posts('showposts=5&offset=1'); ?>
    <section class="archive">
    <?php $i = 0; while (have_posts()) : the_post(); ?>
      <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
            <!-- Post Title -->
        <h1>
            <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
        </h1>
        <!-- /Post Title -->
        <?php html5wp_excerpt('html5wp_index'); // Build your custom callback length in functions.php ?>
    
            <br class="clear">
    
        <?php edit_post_link(); ?>
    
       </article>
       <?php endwhile; ?>
    </section>
    <?php endif; ?>
    
    
    


    这是有道理的,因为您正在将
    放在循环中。你能定义“所有旧帖子”的含义吗?不管是哪种方式,你都需要运行2个循环,一个用于任何不是“旧帖子”的内容,另一个用于“所有旧帖子”。是的,我想发生的是当一篇新帖子被发布时,新帖子会像第一张图片一样被放在下面。答案很棒。非常感谢。我不理解将查询分为两部分的建议。这增加了代码开销(维护问题)和数据库开销(性能问题),这是不必要的,因为我们可以在PHP中轻松区分结果集的第一行和后一行。我遗漏了什么吗?@Sepster-他们不会有任何显著的性能问题(如果有,对结果来说是非常小的)。使用2个查询的原因是,它简化了代码,分离了两个不同的目标,并允许更大的灵活性。尝试将其合并到一个查询中也会不太干净,因为第一篇文章有自己的包装,其余的文章有自己的包装。您必须在循环中包含包装的一半,包装在if语句中。2循环的方法对于他的目标是正确的。可忽略的*n次点击*在多个页面上重复使用类似的模式=性能差的站点。当我们需要全部5行时,我们不会编写SQL语句来一次选择1行。那么,为什么请WP代表我们这样做是可以的呢?一行“if($isFirstPost){”在我看来比重复的样板代码“更干净”,代码目标是非常自解释的。只需要在if块的末尾包含包装的前半部分,它在逻辑上仍然在包装的内容之上。没有“正确的”。这是主观的。但是,有“接受的”:-)嘿,我只是想澄清一下,我重新“接受”的评论是我承认/让步OP接受了你的答案,而不是我的。但是现在在OP将接受转换为我的答案的背景下,让我的评论看起来像是自鸣得意-我不是;-)为什么这是“最好的”?两个单独查询的好处是什么?您的第二个查询将包括第一个查询结果,不是吗?或者WP是否跟踪当前页面已检索到第一篇帖子?hi likeChristan在其代码中指出,您可以将offset参数添加到WP查询中,以将帖子偏移1,即:“showposts=4&offset=1',th从长远来看,这将使管理更容易,运行1个查询以返回您的主要第一篇文章,您可以根据需要设置样式,然后运行第二个偏移查询以生成下一组文章,其中不包括第一篇文章,然后,您可以在不引起太多头痛的情况下设置样式,尝试将您的第一篇文章与剩余的文章分开ing posts,:-)我有这样做的想法,但我不认为确定循环的哪个迭代是一项令人头痛的任务,它值得引起不必要的性能影响和代码重复。lol,不知道为什么会投反对票!!这种方法在我看来是最好的