douang1243 2019-01-26 17:21
浏览 51
已采纳

如果为true,则has_posts()在'single.php'上输出false

Hey fellow WordPress dev's / php masters. Long story short, I am using WordPress (and I never use WordPress - sorry!) and when I go to view the individual post rendered from single.php, the 'have_posts()' function is returning false. The template is rendered and I am not having any issues with the template rendering, I am simply having an issue with 'have_posts()' showing false and not outputting the blog post.

I downloaded WordPress 5.0.3, am running on MAMP and have used this method previously without any issues. All in all, I am trying to really keep my template files organized and I am using get_template_part to retrieve sections of the theme.

My current structure is:

single.php -

<?php
    get_template_part('template-parts/blog/_single/blog-post-banner'); // Renders perfect
?>
<div class="light-grey">
    <div class="container">
        <div class="row no-margin">
            <div class="col s12 l9 mb-0-l">
                <?php get_template_part('template-parts/blog/_single/blog-post'); // Renders perfect ?>
            </div>
            <div class="col s12 l3 mb-0-l">
                <?php get_template_part('template-parts/blog/side-bar'); // Renders perfect ?>
            </div>
        </div>
    </div>
</div>

And of course the real culprit the blog-post partial -

<?php 
if (have_posts()) : 
    the_post(); ?>
    <div class="row no-margin">
        <div class="col s12 no-margin relative">
            <img src="<?php the_post_thumbnail_url('medium_size'); ?>" alt="<?php the_title(); ?>" class="block">
        </div>
        <div class="col s12">
            <div class="white p-2-s p-3-l">
                <?php the_content(); ?>
            </div>
        </div>
    </div>
<?php else :?>
    <div class="col s12 no-margin">
        <div class="white p-2-s p-3-l"><p>Sorry, we couldn't find the post you were looking for.</p></div>
    </div>
<?php endif; ?>

Again, the templates are rendering perfect but the 'have_posts()' is returning false and spitting out my 'else' part of my statement. I have used this method in another template and it worked perfectly before (although I will admit, I love to develop in node and WordPress and php have really made me beat my head against the wall at times lol). Is there something I missed?

I have checked my functions.php which I have written from scratch, and there isn't a single thing in there that might affect the post but if you need to see it, I would be happy to share it (I have disabled each portion of the functions.php to check if it would help and no luck). I don't have any plugins added, not even the default included ones (I am a dork that wants to build most everything from scratch so I know how it works).

Am I wrong in that the output to 'have_posts()' should be true on the single.php page? Did I do something wrong with my partials? The banner does use the 'the_title()' tag and 'the_date()' tag but both php tags are properly closed. Would really appreciate some insight on why I am getting false on this. Thanks everyone!

** EDIT ** - When I went to debug the blog-post partial and var_dump the have_posts(), I placed it above the if statement and everything rendered. Looked like so:

<?php 
var_dump(have_posts()); // <---------------- Added this and it rendered
if (have_posts()) : 
    the_post(); ?>

The var_dump output false but then if I did it a second time, it now renders true. Any ideas as to why? For the time being, I am storing a have_posts() in a random variable so there is no output and the post is now showing. While I am happy the post is now showing, I know this isn't necessarily a 'solid fix'. Any ideas with this?

  • 写回答

1条回答 默认 最新

  • doupin2013 2019-01-26 19:15
    关注

    So first I just want to say, @Jeppe you had it right the whole time. If I could mark your comment correct (and if you can, please show me how) I would.

    To anyone else who stumbles across this post, it comes down to using the 'rewind_posts()' function. All in all, I have a loop somewhere in my code, that I can't identify as the culprit, that is essentially forcing my 'have_posts()' function to return false because I haven't reset it / rewound it according to WordPress.

    My solution was to simply take my old code:

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

    and add the rewind_posts() function above the if statement:

    <?php 
        rewind_posts(); // <---------- This guy right here.
        if (have_posts) :
            the_post;
    ?> 
    

    This will rewind WordPress's loop and allow have_posts() to print true.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化