单次发布时,Have_Posts()返回FALSE

时间:2016-04-27 作者:Gwenolé Gérard

我对循环的使用有点困惑。这是我的第一个主题,我只使用索引。php当前显示所有我的页面。它可以很好地用于页面,但当我尝试显示单个帖子时,\\u posts返回false。这是索引的代码。php:

<?php
get_header();
?>
<div class="left">
    <?php 
    if ( have_posts() ) :
        // Start the loop.
        while ( have_posts() ) : the_post();
            //echo for debugging
            echo \'index<br>\';
            echo get_the_ID().\'<br>\';
            //function to display page
            field_page_builder_front_display(get_the_ID(),get_post_type(get_the_ID()));
        endwhile;
    else :
        ?> 
        Sorry, this page does not exist
        <?php
    endif;
    ?>
</div>
<?php
//display custom sidebar
get_side_bar();
get_footer();
?>
此索引。php为我的所有单篇文章(文章或自定义文章)返回“抱歉,这不存在”。我认为这是因为循环仅用于显示多个帖子,而文件仅用于显示单个帖子。主题为twentfifteen的php也使用循环,所以我不理解这个问题。

提前感谢您的帮助!

2 个回复
SO网友:JoeMoe1984

Late answer but, one possible scenario this could be happening is if you register two custom post types with the same permalink slug.

You can debug this by doing a var_dump of the global variable $wp_query.

global $wp_query;
var_dump($wp_query);

Check to see if the post_type in the query matches the post type of the page you are currently visiting.

If you find that it does not match then your next step is to check out the arguments where both the post types are being registered. Take a look at the rewrite specifically. If the slug is set to the same value, you will have a conflict between the two custom post types.

Bad

// post type (a)
$args_a = array(
  \'rewrite\' => array(
    \'slug\' => \'hello\'
  ),
);

// post type (b)
$args_b = array(
  \'rewrite\' => array(
    \'slug\' => \'hello\'
  ),
);

Good

// post type (a)
$args_a = array(
  \'rewrite\' => array(
    \'slug\' => \'hello\'
  ),
);

// post type (b)
$args_b = array(
  \'rewrite\' => array(
    \'slug\' => \'world\' // <-- this is now different.
  ),
);

That alone may not fix your issue because you will need to flush the permalinks. To do that, you can just go to the Admin Dashboard > Settings > Permalinks page and click on the save changes button. That will flush the permalinks so they are now updated to be hello and world respectively for your custom post types.

SO网友:Nabeel Khan

您可能没有运行任何POST查询。

尝试此(添加query_posts();)

<?php
get_header();
?>
<div class="left">
    <?php 
    query_posts( $args );
    if ( have_posts() ) :
        // Start the loop.
        while ( have_posts() ) : the_post();
            //echo for debugging
            echo \'index<br>\';
            echo get_the_ID().\'<br>\';
            //function to display page
            field_page_builder_front_display(get_the_ID(),get_post_type(get_the_ID()));
        endwhile;
    else :
        ?> 
        Sorry, this page does not exist
        <?php
    endif;
    ?>
</div>
<?php
//display custom sidebar
get_side_bar();
get_footer();
?>

相关推荐

当in_the_loop()为假时,何时以及为什么is_Single(‘my_cpt’)为真?

我正在使用模板系统的示例代码。此地址的页码:http://project.test/my_cpt/hello-post/.无法理解原因is_singular( \'my_cpt\' ) 是true 虽然in_the_loop() 是false.在页面模板中The Loop "E;“工程”:if ( have_posts() ) { while ( have_posts() ) { the_post(); ?>