php - WordPress 网站上的页面内容(通过 get_template_part)显示不正确

标签 php css wordpress wordpress-theming custom-wordpress-pages

这个问题困扰我很久了,请允许我解释一下。

下面是我做的一个页面的截图

enter image description here

这是该页面的代码:

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(接近我想要的页面布局)截图:

enter image description here

这是该页面的代码:

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

我故意把初中部分换成了 高中部分(我对这道题的真正目标)

关于第二版截图的通知

  1. “高中”标签现在不见了

  2. “Features Videos” 标签现在出现了(这也是我真正想要出现在第一个版本的屏幕截图上但不能出现的标签)。

我该如何解决?

我想要的是:

  1. 位于高中之上的初中部分及其图标和说明
  2. Senior High School 及其图标、描述和最重要的标签
  3. 中心的功能部分标签

初中部的代码(通过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;
}

我错过了什么?为什么不能

  1. “高中”标签与其文字和图标一起出现

  2. “专题视频”也会出现

最佳答案

我终于弄明白哪里出了问题。

在有人评论我应该做一个 var_dump() 之后,我想我应该使用

wp_reset_postdata(); 

在 while 循环之后。所以我最终得到的解决方案是这样的:

                    <?php 
                    endwhile; 
                    wp_reset_postdata(); 
                    ?>

关于php - WordPress 网站上的页面内容(通过 get_template_part)显示不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41268281/

相关文章:

javascript - JQuery Datepicker 删除已填充的日期字段

jquery - 如何使在输入中键入的文本出现在单独的 div 中

javascript - 如何使用 JavaScript 删除 iFrame 中的 HTML 元素

javascript - 如何让 Flask 识别静态文件夹中的 CSS 和 JS 文件?

单击 WordPress 管理页面表单中的提交按钮时,JavaScript 代码不运行

php - IntlDateFormatter::format():datefmt_format:采用数组或整数时间戳值或 DateTime 对象 (SonataSanBox)

php - 实际上有可能使用.send()发送二进制XHR请求吗?

php - Google BigQuery 查询很慢

html - 如何更改 WordPress 页面特定区域的 CSS 样式?

css - 如何将横幅广告移到 960 像素宽度之外