2016-07-20 43 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