2016-08-26 62 views
0

嗨,我想创建一个包含我的一些博客帖子的自定义页面(custom-page.php),但是当我测试循环时,我没有看到任何帖子。它只返回我的自定义页面模板的名称作为h2。Wordpress自定义页面,帖子不显示

我有两个帖子已经发布,但他们没有出现。

我有一个front-page.php,当用户第一次访问我的网站时,用户登陆并且我没有改变阅读标签下的任何设置。

我已经阅读了Wordpress codex,到目前为止找不到任何解决方案。

<?php 
get_header(); 
?> 

<div id="primary" class="content-area"> 
    <main id="main" class="site-main" role="main"> 


<h1>BLOG INDEX PAGE 

    BLOG INDEX PAGE</h1> 


    <?php if (have_posts()) : while (have_posts()) : the_post(); ?> 

    <h2><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2> 
    <?php the_content(); ?> 
    <?php endwhile; else : ?> 

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

    <?php endif; ?> 

    </main><!-- #main --> 
</div><!-- #primary --> 


<?php 
get_footer(); 
?> 
+0

在这页你需要证明你的自定义模板。在着陆页或博客页面? –

+0

既不是,它只是一个页面,其中包含我的一些帖子的片段 –

+0

希望所以你必须在管理面板中设置页面来显示它的权利:) –

回答

1

请点击此代码。

$newsQuery = newWP_Query('post_type=post','post_status=publish'); 
if ($newsQuery->have_posts()) { 
    while ($newsQuery->have_posts()) { 
     $newsQuery->the_post(); 
     echo get_the_title(); 
     echo get_the_excerpt(); 
    } 
} 

和您的完整模板将是这样的。

<?php 
get_header(); 
?> 

<div id="primary" class="content-area"> 
<main id="main" class="site-main" role="main"> 


<h1>BLOG INDEX PAGE 

BLOG INDEX PAGE</h1> 


    <?php 
    $newsQuery = new WP_Query('post_type=post','post_status=publish'); 
    if ($newsQuery->have_posts()) : while ($newsQuery->have_posts()) : $newsQuery->the_post(); ?> 

    <h2><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2> 
    <?php the_content(); ?> 
    <?php endwhile; else : ?> 

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

<?php endif; ?> 

</main><!-- #main --> 
</div><!-- #primary --> 


<?php 
get_footer(); 
?> 
+0

这对我工作,谢谢你!现在我需要找到该文档! –

0

创建一个名为通过WP管理“博客”,然后创建一个名为页面blog.php的主题文件夹中的文件,并写入下面的下面的代码页。

<?php 
get_header(); 
?> 

<div id="primary" class="content-area"> 
    <main id="main" class="site-main" role="main"> 

    <h1>BLOG INDEX PAGE</h1> 

    <?php 
    $args = array(
     'post_type'  => 'post', 
     'post_status' => 'publish', 
     'orderby' => 'id', 
     'order' => 'desc' 
    ); 
    $loop = new WP_Query($args); 
    if($loop->have_posts()) : 
    while ($loop->have_posts()) : $loop->the_post(); ?> 

     <h2><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2> 
     <?php the_content(); ?> 

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


    </main><!-- #main --> 
</div><!-- #primary --> 


<?php 
get_footer(); 
?> 
0
 <?php 
      $args = array(
        'post_type' => 'post', 
        'post_status' => 'publish', 
        'posts_per_page' => -1, 
        'offset' => 0 
      ); 

      $the_query1 = new WP_Query($args); 

      if (count($the_query1->posts)>0) { 
       while ($the_query1->have_posts()) : $the_query1->the_post(); 
        get_template_part('loop-archive-template-location'); 
       endwhile; 


      } 
     ?> 
相关问题