2017-07-25 93 views
2

我對WordPress開發很陌生一直遵循一些在線內容和閱讀文檔的位和bobs,但是我的WP_Query仍然沒有顯示任何內容,並且當我向我的循環中添加else時,else條件顯示。我創建了我想從我究竟錯在這裏做數據庫提取的頁面代碼如下:WP_Query不工作或不顯示文章內容

<section id="home"> 
       <?php 
       $query = new WP_Query(array('pagename' => 'home')); 
       if ($query->have_post()) { 
       while ($query->have_posts()){ 
        $query->the_post(); 
        echo '<div class="entry-content">'; 
        the_content(); 
        echo '</div>'; 
       } 
       } 
       wp_reset_postdata(); 
       ?> 
      </section> 

回答

1

簡單地改變

$query->have_post() 

$query->have_posts() 
+0

非常感謝你下次我應該有一個適當的看看我的代碼 – K4S3

+0

不客氣 –

0

使用以下顯示內容代碼

$args=array(
    'post_type' => 'post' 

); 
$the_query = null; 
// the query 
$the_query = new WP_Query($args); ?> 

<?php if ($the_query->have_posts()) : ?> 

    <!-- pagination here --> 

    <!-- the loop --> 
    <?php while ($the_query->have_posts()) : $the_query->the_post(); ?> 
     <h2><?php the_title(); ?></h2> 
     <div class="entry-content"><?php the_content(); ?></div> 
    <?php endwhile; ?> 
    <!-- end of the loop --> 

    <!-- pagination here --> 

    <?php wp_reset_postdata(); ?> 

<?php else : ?> 
    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p> 
<?php endif; ?>