2016-07-20 42 views
1

完全公开,我是一个PHP noob。 我正在尝试为我的Wordpress网站构建页面模板。这个想法是从今天开始显示每一篇文章,并排除所有其他文章。似乎无法在wordpress页面模板中结束循环

有人帮我建立一些代码,但我有一个新的问题 - 只有从今天开始的最古老的帖子正在显示!我用三分钟测试了几分钟,只有第一个显示出来。

任何帮助表示赞赏。

完整代码如下。

<?php 
/** 
* Template Name: Home Page 
* The template for displaying all of today's posts on home page. 
* 
* This is the template that displays only today's posts. 
* Please note that this is the WordPress construct of pages and that 
* other "pages" on your WordPress site will use a different template. 
* 
* @package WordPress 
* @subpackage Twenty_Fifteen 
* @since Twenty Fifteen 1.0 
*/ 

get_header(); ?> 

<?php 
$today = getdate(); 
$args = array(
    'date_query' => array(
     array(
      'year' => $today['year'], 
      'month' => $today['mon'], 
      'day' => $today['mday'], 
     ), 
    ), 
); 
$query = new WP_Query($args); 
?> 


<div id="primary" class="content-area"> 
    <main id="main" class="site-main" role="main"> 

    <?php 
    // Start the loop. 
    if ($query->have_posts()) 
     echo'<ul>'; 
     while ($query->have_posts()) { 
      $query->the_post(); 
     } 
     echo '</ul>'; 

     // Include the page content template. 
     get_template_part('content', 'page'); 

      // If comments are open or we have at least one comment, load up the comment template. 
      if (comments_open() || get_comments_number()) : 
       comments_template(); 

     /* Restore Post Data */ 
     wp_reset_postdata(); 
     else : // no posts found. 
     ; 

    endif ; 
    ?> 

    </main><!-- .site-main --> 
</div><!-- .content-area --> 

<?php get_footer(); ?> 
+0

从管理员设置每页的帖子到-1 – Mickey

+0

我假设你是指阅读设置。它不会让我使用任何小于0. –

+0

然后使其为零,你面临的概率实际上是分页 – Mickey

回答

0

我最后想出来的!原来,

echo '<li>' . get_template_part('content', 'page') . '</li>'; 

应该在while循环内。

这就是为什么它只显示我第一个。没有分页问题。

之后,唯一的问题是链接不能用于某种原因 - 所以我摆脱了get_template_part函数中'page'的调用。现在一切都很好!

无论如何,感谢所有的帮助!

0
$args = array(
    'date_query' => array(
     array(
      'year' => $today['year'], 
      'month' => $today['mon'], 
      'day' => $today['mday'], 
     ), 
    ), 
    'posts_per_page' => -1 
); 
$query = new WP_Query($args); 
+0

无变化:/ 也许我的其他一些设置有所作为? 主题:二十五 我已经设置了特定的首页作为我的首页,所以我可以应用自定义页面模板的php文件。 –

+0

尽管这段代码可能会解决这个问题,但[包括解释](// meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers)确实有助于提高帖子的质量。请记住,您将来会为读者回答问题,而这些人可能不知道您的代码建议的原因。也请尽量不要使用解释性注释来挤占代码,因为这会降低代码和解释的可读性! – FrankerZ

+0

@dovid,哪一个是你的主页? index.php?或者你可以检查任何静态页面? – Mickey