这个问题困扰我很久了,请允许我解释一下。
下面是我做的一个页面的截图
这是该页面的代码:
get_header(); ?>
<?php
get_template_part('content', 'hero');
?>
<?php
get_template_part('content', 'welcome');
?>
<?php
get_template_part('content', 'senior');
get_template_part('content', 'jh');
get_template_part('content', 'our-videos');
?>
<?php
get_footer();
下面是版本 2(接近我想要的页面布局)截图:
这是该页面的代码:
get_header(); ?>
<?php
get_template_part('content', 'hero');
?>
<?php
get_template_part('content', 'welcome');
?>
<?php
get_template_part('content', 'jh');
get_template_part('content', 'senior');
get_template_part('content', 'our-videos');
?>
<?php
get_footer();
我故意把初中部分换成了 高中部分(我对这道题的真正目标)
关于第二版截图的通知
“高中”标签现在不见了
“Features Videos” 标签现在出现了(这也是我真正想要出现在第一个版本的屏幕截图上但不能出现的标签)。
我该如何解决?
我想要的是:
- 位于高中之上的初中部分及其图标和说明
- Senior High School 及其图标、描述和最重要的标签
- 中心的功能部分标签
初中部的代码(通过WP的get_template_part()函数)如下:
<?php
$junior_high_feature_title = get_field('junior_high_feature_title');
$junior_high_feature_body = get_field('junior_high_feature_body');
?>
<!-- Project -->
<section id="senior_high_features">
<div class="container">
<h2><?php echo $junior_high_feature_title; ?></h2>
<p class="lead">
<?php echo $junior_high_feature_body; ?>
</p>
<div class="row">
<?php $loop = new WP_Query( array( 'post_type' => 'junior_high_school', 'orderby' => 'post_id',
'order' => 'ASC' ) ); ?>
<?php while( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="col-sm-4">
<?php
if( has_post_thumbnail() )
{
the_post_thumbnail();
}
?>
<h3><?php the_title(); ?></h3>
<p><?php the_content(); ?></p>
</div><!-- col -->
<?php endwhile; ?>
</div><!-- row -->
</div><!-- container -->
</section>
高中部分(通过 WP 的 get_template_part() 函数)的代码如下
<?php
$senior_high_school_features_title = get_field('senior_high_school_features_title');
$senior_high_school_features_body = get_field('senior_high_school_features_body');
?>
<!-- PROJECT FEATURES
================================================== -->
<section id="senior_high_features">
<div class="container">
<h2><?php echo $senior_high_school_features_title; ?></h2>
<p class="lead"><?php echo $senior_high_school_features_body; ?></p>
<div class="row">
<?php $loop = new WP_Query( array( 'post_type' => 'senior_high_school', 'orderby' => 'post_id', 'order' => 'ASC' ) ); ?>
<?php while( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="col-sm-4">
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
?>
<h3><?php the_title(); ?></h3>
<p><?php the_content(); ?></p>
</div><!-- end col -->
<?php endwhile; wp_reset_query(); ?>
</div><!-- row -->
</div><!-- container -->
</section><!-- project-features -->
senior_high_features 部分的 CSS 是:
#senior_high_features
{
text-align: center;
}
.section
{
padding: 80px 0;
}
我错过了什么?为什么不能
“高中”标签与其文字和图标一起出现
“专题视频”也会出现
最佳答案
我终于弄明白哪里出了问题。
在有人评论我应该做一个 var_dump() 之后,我想我应该使用
wp_reset_postdata();
在 while 循环之后。所以我最终得到的解决方案是这样的:
<?php
endwhile;
wp_reset_postdata();
?>
关于php - WordPress 网站上的页面内容(通过 get_template_part)显示不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41268281/