2016-08-24 77 views
0
 <?php 
     $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; 
     $args = array(
      'post_type' => 'post', 
      'posts_per_page' => 7, 
      'paged' => $paged); 
     $wp_query = new WP_Query($args); 
     $count = 0; 

     while (have_posts()) : the_post(); ?> 
      <?php echo get_the_title(); ?> 
      <?php 
       $count++; 
       if ($count === 1) { ?> 
        <div class="column small-12 margin-bottom-small"> 
         <div class="panel-brown"> 
          <img src="" alt="" /> 
          <h5 class="heading-white"><a href="#"><?php echo get_the_title(); ?></a></h5> 
          <div class="para-white"> 
           <?php $content = get_the_content(); echo mb_strimwidth($content, 0, 200, '...');?> 
          </div> 
         </div> 
        </div> 
      <?php } 
       while ($count <= 2) { 
        $count++ ?> 
        <div class="column small-12 medium-6 margin-bottom-small"> 
         <div class="panel-tan"> 
          <h5 class="heading-white"><a href="#"><?php echo get_the_title(); ?></a></h5> 
          <div> 
           <?php $content = get_the_content(); echo mb_strimwidth($content, 0, 200, '...');?> 
          </div> 
         </div> 
        </div> 
      <?php } 
       while ($count <= 4) { 
        $count++ ?> 
        <div class="column small-12 medium-6 margin-bottom-small"> 
         <div class="panel-tan"> 
          <h5 class="heading-white"><a href="#"><?php echo get_the_title(); ?></a></h5> 
          <div> 
           <?php $content = get_the_content(); echo mb_strimwidth($content, 0, 200, '...');?> 
          </div> 
         </div> 
        </div> 
      <?php } 
      ?> 
     <?php endwhile; ?> 

     <!-- Pagination links --> 
     <?php next_posts_link('&larr; Older posts', $wp_query ->max_num_pages); ?> 
     <?php previous_posts_link('Newer posts &rarr;'); ?> 

我挣扎理解为什么if和while语句循环相同的职位,但直接在此行之后:PHP - 同一职位而非显示接下来的文章中

while (have_posts()) : the_post(); 

我在哪里回应标题,而是显示帖子列表。

我想在这里实现的是为循环中输出的每个帖子创建不同的html布局。

干杯

+0

你的意思是有第一个语句'如果$计数=== 1'但后来有'而$计数> = 2'?似乎这些都应该是如果陈述。我也看不出你是如何在主循环中使用WP_Query的结果......可能是手头的问题 – Chizzle

回答

1

我认为你是在你的查询结果试图环如图所示WP_Query official documentation

此外,不知道为什么你会这样做,而像你这样的循环内主后循环本身,所以我改变了这些if语句。

像这样的东西是什么,我相信你是后:

<?php 
     $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; 
     $args = array(
      'post_type' => 'post', 
      'posts_per_page' => 7, 
      'paged' => $paged); 
     $wp_query = new WP_Query($args); 
     $count = 0; 

     if(! $wp_query->have_posts()) 
      die('no posts!'); // ADDED - Handle this more elegantly 

     while ($wp_query->have_posts()) : $wp_query->the_post(); // ADDED - Use query result object?> 
      <?php echo get_the_title(); ?> 
      <?php 
       $count++; 
       if ($count === 1) { ?> 
        <div class="column small-12 margin-bottom-small"> 
         <div class="panel-brown"> 
          <img src="" alt="" /> 
          <h5 class="heading-white"><a href="#"><?php echo get_the_title(); ?></a></h5> 
          <div class="para-white"> 
           <?php $content = get_the_content(); echo mb_strimwidth($content, 0, 200, '...');?> 
          </div> 
         </div> 
        </div> 
      <?php } 
       if ($count <= 2) { 
        ?> 
        <div class="column small-12 medium-6 margin-bottom-small"> 
         <div class="panel-tan"> 
          <h5 class="heading-white"><a href="#"><?php echo get_the_title(); ?></a></h5> 
          <div> 
           <?php $content = get_the_content(); echo mb_strimwidth($content, 0, 200, '...');?> 
          </div> 
         </div> 
        </div> 
      <?php } 
       if ($count <= 4) { // ADDED - Be careful, this will fire as well as the if statement above it since they are both <= 2 
        ?> 
        <div class="column small-12 medium-6 margin-bottom-small"> 
         <div class="panel-tan"> 
          <h5 class="heading-white"><a href="#"><?php echo get_the_title(); ?></a></h5> 
          <div> 
           <?php $content = get_the_content(); echo mb_strimwidth($content, 0, 200, '...');?> 
          </div> 
         </div> 
        </div> 
      <?php } 
      ?> 
     <?php endwhile; ?> 

     <!-- Pagination links --> 
     <?php next_posts_link('&larr; Older posts', $wp_query ->max_num_pages); ?> 
     <?php previous_posts_link('Newer posts &rarr;'); ?> 
+0

我试图使用while循环,所以我不必有两个if语句来减少我的代码。让我看看这个解决方案是否工作。谢谢 –

+0

对不起,这仍然输出相同的帖子,而不是循环所有的帖子。 –

+0

啊我想通了。 –

0

这是不工作的原因是因为我在while循环意味着它永远不会是右边数调用多个地方计数++ 。我通过在if和while循环之外放置一次来解决这个问题。

感谢你的努力的家伙

相关问题