简体   繁体   中英

Why it gives random post ID

I am building a WordPress theme. I am having some trouble on commenting section. When showing comments it gets randomly post ID.

I have put the same code in 2 different places in the first place it TOP but by at BOTTOM its not working. Can anyone help me by telling why is this not working at bottom?!

Here is my single.php file

<?php get_template_part('/template-parts/standard-post-header');  ?>
    <main role="main">
        <!-- section -->
        <section>
            <div class="container background-color">
                <?php if (have_posts()): while (have_posts()) : the_post(); ?>

                <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>                   

                    <?php if (has_post_video( isset($post_id) ) != null) {
                    // Featured VIDEO -->
                        get_template_part('/template-parts/featured-video-post');
                    // END Featured VIDEO -->   
                    } else { ?>

                    <!-- Featured Image -->
                    <?php $header_type = get_post_meta(get_the_id(), 'meta-box-dropdown', true); ?> 

                    <?php if ($header_type == 'Slider') { 
                    // SLIDER Header
                        get_template_part('/template-parts/featured-slider-post');
                    ?>

                    <?php } else { 
                    // SLIDER Header
                        get_template_part('/template-parts/featured-normal-post');
                    } ?>                        
                    <!-- END Featured Image --> 
                    <?php } ?>

                    <div class="container container-post-color">
                        <div class="content">
                            <?php the_content(); ?>
                            <?php edit_post_link(); ?>
                        </div>
                    </div>

                    <?php 
                    global $post;
                    echo $post->ID;
                    ?>

                    <ol class="commentlist">
                        <?php
                            //THIS WORKS!!!
                            $comments = get_comments(array(
                                'post_id' => $post->ID,
                                'status' => 'approve'
                            ));


                            wp_list_comments(array(
                                'per_page' => 10, 
                                'reverse_top_level' => false 
                            ), $comments);
                        ?>
                    </ol>   

                    <!-- Post Navigation -->
                    <div class="container container-post-color top-space" style="padding-left: 0px; padding-right: 0px;">
                        <div id="left-side"><?php previous_post_link(); ?></div>
                        <div id="right-side"><?php next_post_link(); ?></div>
                        <?php echo wp_link_pages(); ?>
                    </div>

                    <!-- Tags -->
                    <div class="tags-area">
                        <?php echo the_tags(); ?>
                    </div>

                    <!-- Related Articles -->
                    <?php get_template_part('/template-parts/related-articles'); ?>

                    <!-- Coments Part -->               
                    <?php //get_template_part('/template-parts/comments'); ?>

                    <?php 
                    global $post;
                    echo $post->ID;
                    ?>

                    <ol class="commentlist">
                        <?php
                            //THIS DOES NOT WORKS!!! WHY?!
                            $comments = get_comments(array(
                                'post_id' => $post->ID,
                                'status' => 'approve' 
                            ));


                            wp_list_comments(array(
                                'per_page' => 10,
                                'reverse_top_level' => false 
                            ), $comments);
                        ?>
                    </ol>                   

                </article>
                <!-- /article -->
            </div>
            <!-- END of Container-Fluid -->

            <?php endwhile; ?>
            <?php else: ?>

            <!-- article -->
            <article>
                <div class="container background-color">
                    <h1 class="black mastheading-post"><?php _e( 'Sorry, nothing to display.', 'html5blank' ); ?></h1>
                </div>
            </article>
            <!-- /article -->

            <?php endif; ?>
        </section>
        <!-- /section -->
    </main>


    <!-- INSTAGRAM -->
    <?php get_template_part('/template-parts/instagram'); ?>


<?php get_footer(); ?>

related-articles.php

<div class="container container-post-color" style="padding-left: 0px; padding-right: 0px;">
    <div class="random">                
            <ul>
                <?php $posts = get_posts('orderby=rand&numberposts=4'); foreach($posts as $post) { ?>
                <div class="col-md-3 padding-zero">
                    <li>
                        <div class="random-thumb">
                            <?php the_post_thumbnail('full'); ?>
                        </div>
                        <div class="random-title">
                            <h1 class="black mastheading"><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h1>
                        </div>

                        <div class="black iltalica"><?php echo excerpt(25); ?></div>

                        <div class="category">
                            <div class="random-category">
                                <h5><?php echo the_category();?></h5> 
                            </div>
                        </div>

                    </li>
                </div> 
                <?php } ?>
            </ul>   
    </div>
</div>

So first off, since you are in the loop, everywhere you've used global $post; ... $post->ID global $post; ... $post->ID you should be able to use get_the_ID() instead.

Second, I strongly suspect the problem is your template part /template-parts/related-articles probably messes-up the main loop . I suggest you look at that file and see if it's itself looping on a selection of posts - chances are it is not doing it cleanly, in a way that can be re-used inside the main loop.

You can add that file's code to your question if you need help figuring it out.

UPDATE

Ok, so indeed, you need to reset the loop data after the related-articles loop:

...
<?php
    }
    wp_reset_postdata(); // <----- add this after your loop
?>
</ul>
...

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM