为什么它提供随机帖子ID

时间:2017-01-18 19:41:05

标签: php wordpress html5 wordpress-theming

我正在构建一个WordPress主题。我在评论部分遇到了一些麻烦。在显示评论时,它会随机发布ID。

我把相同的代码放在两个不同的地方,它在TOP的第一个地方,但在BOTTOM它不起作用。任何人都可以告诉我为什么这不起作用?!

这是我的 single.php 文件

<?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(); ?>

相关-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>

1 个答案:

答案 0 :(得分:2)

首先,由于您处于循环中,在您使用global $post; ... $post->ID的任何地方,您应该能够使用get_the_ID()

其次,我强烈怀疑问题是你的模板部分/template-parts/related-articles可能会混淆main loop。我建议你查看一下这个文件,看看它本身是否会在一系列帖子中循环 - 很可能它不是干净利落的,可以在主循环中重复使用。

如果您需要帮助解决问题,可以将该文件的代码添加到您的问题中。

<强>更新

好的,确实需要在相关文章循环之后重置循环数据:

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

希望这有帮助!