theme development - have_posts() return false on single post

admin2025-01-07  3

I'm a little bit confused with the use of the Loop. This is my first theme and i just use index.php currently to display all my pages. It works fine for pages but have_posts return false when i try to display a single post. Here is the code of the index.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();
?>

This index.php return "Sorry, this does not exist" for all my single post (articles or custom post). I thought it was because the loop is only for displaying multiple posts but the file single.php of the theme twentfifteen use the loop too, so i don't understand the problem.

Thank in advance for all your help !

I'm a little bit confused with the use of the Loop. This is my first theme and i just use index.php currently to display all my pages. It works fine for pages but have_posts return false when i try to display a single post. Here is the code of the index.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();
?>

This index.php return "Sorry, this does not exist" for all my single post (articles or custom post). I thought it was because the loop is only for displaying multiple posts but the file single.php of the theme twentfifteen use the loop too, so i don't understand the problem.

Thank in advance for all your help !

Share Improve this question asked Apr 27, 2016 at 9:19 Gwenolé GérardGwenolé Gérard 11 bronze badge 3
  • does it work when you make a copy of index.php and call it single.php? might not solve it but it's worth a try. – majick Commented Apr 27, 2016 at 9:31
  • index.php for work both single post, page or custom post type if single.php file not meet then default template call index.php. Look wordpress template hierarchy developer.wordpress.org/files/2014/10/template-hierarchy.png. – user5200704 Commented Apr 27, 2016 at 9:38
  • If you have just started theme development I highly recommend this YouTube Tutorial on WordPress Theme Development - it explains all the basics of what WordPress Templates are. – Stephen Commented Apr 27, 2016 at 12:07
Add a comment  | 

2 Answers 2

Reset to default 0

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.

You don't have any posts query running probably.

Try this (added 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();
?>
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736264565a978.html

最新回复(0)