簡體   English   中英

WordPress:使用頁面模板在wp頁面中獲取帖子

[英]WordPress : using page template to fetch posts in wp page

我想在wp頁面中顯示帖子。 為此,我所做的就是創建了一個名為mypage-page.php的模板,並將代碼從page.php復制到mypage-page.php。

這是我的mypage-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 ,我希望它應該列出帖子,但沒有,請幫助我了解我錯了。 我只是一個初學者

如果您的頁面是“自定義頁面”模板,則您已經初始化了一個自定義查詢。 WordPress的默認查詢不適用於自定義頁面模板。

您可以使用WP_Query類進行該查詢: https ://codex.wordpress.org/Class_Reference/WP_Query

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

我建議較早的一個。

您也可以使用此query_posts

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM