Php 为什么它会给出随机的帖子ID
我正在构建一个WordPress主题。我在评论部分遇到了一些问题。当显示评论时,它会随机获得帖子ID 我已经把相同的代码放在两个不同的地方,第一个地方是顶部,但底部是不起作用的。谁能告诉我为什么这在底部不起作用 这是我的single.php文件Php 为什么它会给出随机的帖子ID,php,wordpress,html,wordpress-theming,Php,Wordpress,Html,Wordpress Theming,我正在构建一个WordPress主题。我在评论部分遇到了一些问题。当显示评论时,它会随机获得帖子ID 我已经把相同的代码放在两个不同的地方,第一个地方是顶部,但底部是不起作用的。谁能告诉我为什么这在底部不起作用 这是我的single.php文件 <?php get_template_part('/template-parts/standard-post-header'); ?> <main role="main"> <!-- section
<?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(); ?>
首先,由于您处于循环中,所以无论您在哪里使用global$post$post->ID
您应该可以使用获取ID()
第二,我强烈怀疑问题在于您的模板部分/template parts/相关文章可能会把整个过程搞砸。我建议你看看这个文件,看看它本身是否在一系列帖子上循环——很可能它做得不干净,在主循环中可以重复使用
如果需要帮助,可以将该文件的代码添加到问题中
更新
好的,实际上,您需要在相关文章循环之后重置循环数据:
...
<?php
}
wp_reset_postdata(); // <----- add this after your loop
?>
</ul>
...
。。。
...
希望这有帮助 谢谢你的评论!我将把related-articles.php放在我最初的问题上。是的,你是对的。。一旦我对此发表评论,它就开始工作了。。。谢谢你的帮助!
...
<?php
}
wp_reset_postdata(); // <----- add this after your loop
?>
</ul>
...