2015-10-11 62 views
0

我想在WP頁面中顯示帖子。要做到這一點,這是我做了什麼,我創建了一個名爲我的空間-page.php文件模板,複製代碼page.php文件我的空間-page.php文件WordPress的:使用頁面模板來獲取WP中的帖子

這是我的我的空間-page.php文件

  <main id="main" class="site-main" role="main"> 
        <?php if (have_posts()) : while (have_posts()) : the_post(); ?> 

          <h2><?php the_title() ;?></h2> 
          <?php the_post_thumbnail(); ?> 
          <?php the_excerpt(); ?> 

        <?php endwhile; else: ?> 

          <p>Sorry, no posts to list</p> 

        <?php endif; ?> 

     </main><!-- .site-main --> 
     // this has header and footer as well . 

現在我使用這個模板,我希望它應該列出的職位,但它沒有,請幫助我明白的地方我錯創建了一個頁面mytest的。 我只是一個初學者

+0

您可能必須重置發佈對象。這個問題可以住在這裏,但你可能會發現在wordpress.stackexchange.com – Shawn

回答

2

如果你的頁面是一個自定義頁面模板,那麼你有初始化一個自定義查詢。 WordPress的默認查詢在自定義頁面模板中不起作用。

可以使用WP_Query類做查詢: https://codex.wordpress.org/Class_Reference/WP_Query

或者 get_posts(): https://codex.wordpress.org/Template_Tags/get_posts

我建議較早的企業之一。

+0

yes試驗,它的工作 – Sikander

+0

更多的事情我創建模板命名爲page-mytest,它只提取後名爲mytest。什麼應該是模板的名稱,以便它可以呈現所有帖子?或者還有其他問題 – Sikander

相關問題