我的网站上的 single.php 文件遇到问题。通常,它应该显示每个帖子的正确信息。然而,我注意到,当一个类别只有一个帖子时,一切都会按预期进行。但是,一旦我向该类别添加第二篇文章,显示的数据就会变得不准确。为了更清楚地说明问题,我附上了一些图片。
第1篇标题:他喜欢我吗?
第 1 条鼻涕虫:他喜欢我吗
第 2 篇文章标题:我准备好建立关系了吗?
发布 2 Slug:我准备好建立关系了
但是您可以在 url slug 中看到不同的内容,并且帖子标题显示不同的
还要检查第二个屏幕截图中的类别,该屏幕截图将包含2个类别发布到问题中。
我的 single.php 代码:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="single-header-2 single-header-top-full row no-gutters mb-50 background12">
<div class="col-lg-6 col-md-12">
<figure class="single-thumnail img-hover-slide mb-0"
style="background-image: url(<?php echo get_the_post_thumbnail_url(); ?>)">
</figure>
</div>
<div class="col-lg-6 col-md-12">
<div class="entry-header entry-header-1">
<div class="entry-meta meta-0 font-small mb-30">
<?php
$categories = get_the_category();
if ( ! empty( $categories ) ) {
foreach ( $categories as $category ) {
echo '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '">';
echo ' <span class="post-cat background7 color-white">' . esc_html( $category->name ) . '</span>';
echo '</a>';
}
}
?>
</div>
<h1 class="post-title">
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</h1>
<div class="entry-meta meta-1 font-small color-grey mt-15 mb-15 d-flex">
<span class="post-by">
<?php
printf( esc_html__( 'By %s', 'text-domain' ), '<a href="' . esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ) . '">' . get_the_author() . '</a>' );
?>
</span>
<span class="post-on has-dot"><?php echo get_the_date(); ?></span>
<span class="time-reading has-dot"><?php echo reading_time(); ?></span>
<span class="hit-count">
<i class="ti-bolt"></i>
<?php echo get_post_meta( get_the_ID(), 'views', TRUE ); ?><?php esc_html_e( 'views', 'text-domain' ); ?>
</span>
</div>
<div class="bt-1 border-color-1 mt-30 mb-30"></div>
<div class="single-social-share clearfix ">
<ul class="d-inline-block list-inline float-right">
<li class="list-inline-item">
<a class="social-icon facebook-icon text-xs-center color-white" href="javascript:void(0)"
onclick="window.open('https://www.facebook.com/sharer/sharer.php?u=<?php the_permalink(); ?>&title=<?php the_title_attribute(); ?>', 'Share This Post', 'width=640,height=450'); return false">
<i class="ti-facebook"></i>
</a>
</li>
<li class="list-inline-item">
<a class="social-icon twitter-icon text-xs-center color-white" href="javascript:void(0)"
onclick="window.open('https://twitter.com/intent/tweet?url=<?php the_permalink(); ?>&text=<?php the_title_attribute(); ?>', 'Share This Post', 'width=640,height=450'); return false">
<i class="ti-twitter-alt"></i>
</a>
</li>
<li class="list-inline-item">
<a class="social-icon instagram-icon text-xs-center color-white" href="javascript:void(0)"
onclick="window.open('https://www.linkedin.com/shareArticle?mini=true&url=<?php the_permalink(); ?>&summary=<?php the_title_attribute(); ?>', 'Share This Post', 'width=640,height=450'); return false">
<i class="ti-linkedin"></i>
</a>
</li>
<li class="list-inline-item">
<a class="social-icon pinterest-icon text-xs-center color-white" href="javascript:void(0)"
onclick="window.open('http://pinterest.com/pin/create/button/?url=<?php the_permalink(); ?>&summary=<?php the_title_attribute(); ?>', 'Share This Post', 'width=640,height=450'); return false">
<i class="ti-pinterest"></i>
</a>
</li>
</ul>
</div>
</div>
</div>
</div>
<?php endwhile; ?>
<?php endif; ?>
经过一番研究,我发现问题出在 header.php 文件中。这是独家新闻:
类别链接问题:此部分获取类别并为每个类别创建链接。这里看起来一切都很好。 帖子获取循环:它使用 get_posts() 获取每个类别的帖子。看起来也不错。 帖子数据混合:但是当它尝试显示标题和 URL 等帖子信息时,它会变得混乱。问题是它没有在每次循环后正确重置发布数据。因此,它最终会显示来自错误帖子的数据。 要解决此问题,只需在输出帖子的循环后面添加 wp_reset_postdata() 即可。这将确保它每次都能获取正确帖子的数据。
$categories = get_categories(array(
'orderby' => 'rand',
'number' => 5,
));
echo '<li class="mega-menu-item">';
echo '<a href="' . esc_url(home_url('/category')) . '">Categories</a>';
echo '<div class="sub-mega-menu">';
echo '<div class="nav flex-column nav-pills" role="tablist">';
foreach ($categories as $category) {
$class = ($category === reset($categories)) ? 'active show' : '';
echo '<a class="nav-link ' . $class . '" data-toggle="pill" href="' . esc_url(get_category_link($category->term_id)) . '" role="tab">' . $category->name . '</a>';
}
echo '</div>';
echo '<div class="tab-content">';
foreach ($categories as $index => $category) {
$posts = get_posts(array(
'category' => $category->term_id,
'numberposts' => 4,
));
$class = ($index == 0) ? 'active show' : '';
echo '<div class="tab-pane ' . $class . '" id="news-' . $index . '" role="tabpanel">';
echo '<div class="row">';
foreach ($posts as $post) {
setup_postdata($post);
echo '<div class="col-3 post-module-1">';
echo '<div class="post-thumb d-flex border-radius-5 img-hover-scale mb-15">';
echo '<a href="' . esc_url(get_permalink()) . '">';
echo get_the_post_thumbnail($post->ID, 'thumbnail');
echo '</a>';
echo '</div>';
echo '<div class="post-content media-body">';
echo '<h6 class="post-title mb-10 text-limit-2-row">' . get_the_title() . '</h6>';
echo '<div class="entry-meta meta-1 font-x-small color-grey">';
echo '<span class="post-on">' . get_the_date() . '</span>';
echo '<span class="hit-count has-dot">' . get_post_meta($post->ID, 'views', true) . ' Views</span>'; // Assuming 'views' is the custom field for post views
echo '</div>';
echo '</div>';
echo '</div>';
}
echo '</div>';
echo '</div>';
wp_reset_postdata(); // Reset post data to avoid conflicts
}
echo '</div>';
echo '</div>';
echo '</li>';
?>