2016-12-07 74 views
0

我想在稱爲「文章」的特定頁面上顯示所有帖子。如何獲取WordPress中特定頁面上的所有帖子標題

但是,當我在循環中使用the_title();時,看起來這個頁面的標題被顯示。在articles.php

相對碼(鏈接到儀表盤創建的Articles頁)

<div class="container"> 
    <?php if (have_posts()) : ?> 
     <div class="content"> 
      <?php while (have_posts()) : the_post(); ?>     
       <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>     
        <?php get_template_part('content', get_post_format()); ?>            
       </div>              
      <?php endwhile; ?> 
     </div> 
    <?php endif; ?> 
</div> 

content.php

<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="post-header section medium-padding"> 
    <div class="post-meta-top"> 
     <?php the_time(get_option('date_format')); ?> 

     <?php 
      if (comments_open()) { 
       echo '<span class="sep">/</span> '; 
       if (is_single()) 
        comments_popup_link('0 comments', '1 comment', '% comments', 'post-comments'); 
       else 
        comments_number('0 comments', '1 comment', '% comments'); 
      } 
     ?> 
    </div> 

    <h2 class="post-title"><?php the_title(); ?></h2>   
</a> 

這部分工作良好index.php

+0

你可以發佈你的循環代碼嗎? –

+0

@Johann Kratzik我已經更新了我的問題。這部分工作在'index.php'中,但在'articles.php'中有問題,用作** Dashboard **中創建的頁面「文章」**的模板頁面。 –

+0

這是你的全部代碼嗎?您需要使用頁面模板中的WP_Query()定義循環本身。否則只會顯示當前頁面循環。 –

回答

1

步驟1:創建content.php的文件夾內的一個新的模板articles.php和插入的代碼:

<?php 
$articles = new WP_Query(array('posts_per_page' => -1)); 

while ($articles->have_posts()) : 
$articles->the_post(); 
?> 

<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="post-header section medium-padding"> 
<div class="post-meta-top"> 
    <?php the_time(get_option('date_format')); ?> 

    <?php 
     if (comments_open()) { 
      echo '<span class="sep">/</span> '; 
      if (is_single()) 
       comments_popup_link('0 comments', '1 comment', '% comments', 'post-comments'); 
      else 
       comments_number('0 comments', '1 comment', '% comments'); 
     } 
    ?> 
</div> 

<h2 class="post-title"><?php the_title(); ?></h2>   
</a> 
<?php endwhile; ?> 

步驟2:在articles.php替換這一行:

<?php get_template_part('content', get_post_format()); ?> 

這一個:

<?php get_template_part('articles'); ?> 

未經測試,但它應該工作。

+0

感謝您的幫助。它現在有效! –

+0

太棒了:)我很高興聽到 –

相關問題