【发布时间】:2020-08-30 01:25:06
【问题描述】:
我正在学习一门课程,并与讲师一起编写了一个自定义网站,但我无法弄清楚为什么资源页面 (https://topcdltraining.com/resources/) 和联系页面 (https://topcdltraining.com/contact/) 上的标题图片较短它应该与博客页面上的标题图像高度相同(https://topcdltraining.com/blog/)。有人可以帮我看看我在哪里搞砸了吗?关于特征图像和 H1 标题,我对所有三个页面都有完全相同的 html 代码(我什至再次在所有三个页面上复制并粘贴了相同的代码,并更改了标题以确保)。我为静态资源页面以及下面的 page-resources.php 页面添加了 HTML、CSS 和 PHP 代码(请忽略我添加到 PHP 代码中的 cmets - 我对此很陌生,所以我添加了 cmets 以提供帮助我记得以后怎么做这一切)。
```
<section class="feature-image feature-image-default-alt" data-type="background" data-speed="2">
<h1 class="page-title">Blog</h1>
</section><!-- feature-image -->
============================= CSS ======================================
.feature-image {
display: table;
width: 100%;
}
.feature-image-default {
background: url('/wp-content/themes/bootstrap2wordpress/assets/img/stuff-feature.jpg') no-repeat;
background-size: cover;
}
.feature-image-default-alt {
background: url('/wp-content/themes/bootstrap2wordpress/assets/img/hipster-stuff.jpg') no-repeat;
background-size: cover;
}
.feature-image h1 {
display: table-cell;
vertical-align: middle;
text-align: center;
color: white;
}
============================== PHP ==========================================
<?php if( has_post_thumbnail() ) { // add php code to check for featured image ?>
<!-- add php to allow featured image to be shown by url -->
<section class="feature-image" style="background:url('<?php echo $thumbnail_url; ?>') no-repeat; background-size: cover;" data-type="background" data-speed="2">
<!-- add php code to make title dynamic -->
<h1><?php the_title(); ?></h1>
</section><!-- feature-image -->
<?php } else { // add php code for fallback image if there is no featured image ?>
<section class="feature-image feature-image-default" data-type="background" data-speed="2">
<!-- add php code to make title dynamic -->
<h1><?php the_title(); ?></h1>
</section><!-- feature-image -->
<?php } ?>
============== 讲师的 PHP 代码 ======================
<?php
/* Template Name: Resources Page */
get_header();
$thumbnail_url = wp_get_attachment_url( get_post_thumbnail_id( $post->ID ) );
?>
<!-- FEATURE IMAGE
================================================== -->
<?php if( has_post_thumbnail() ) { // check for feature image ?>
<section class="feature-image" style="background: url('<?php echo $thumbnail_url; ?>') no-repeat; background-size: cover;" data-type="background" data-speed="2">
<h1 class="page-title"><?php the_title(); ?></h1>
</section>
<?php } else { // fallback image ?>
<section class="feature-image feature-image-default" data-type="background" data-speed="2">
<h1 class="page-title"><?php the_title(); ?></h1>
</section>
<?php } ?>
<!-- MAIN CONTENT
================================================== -->
<div class="container">
<div class="row" id="primary">
<div id="content" class="col-sm-12">
<section class="main-content">
<?php while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; // end of the loop ?>
<?php $loop = new WP_Query( array( 'post_type' => 'resource', 'orderby'=>'post_id', 'order'=>'ASC' ) ); ?>
<hr>
<div class="resource-row clearfix">
<?php while( $loop->have_posts() ) : $loop->the_post(); ?>
<?php
$resource_image = get_field('resource_image');
$resource_url = get_field('resource_url');
$button_text = get_field('button_text');
?>
<div class="resource">
<img src="<?php echo $resource_image[url]; ?>" alt="<?php echo $resource_image[alt]; ?>">
<h3><a href="<?php echo $resource_url; ?>"><?php the_title(); ?></a></h3>
<?php the_content(); ?>
<?php if( !empty($button_text) ) : ?>
<a href="<?php echo $resource_url; ?>" class="btn btn-success"><?php echo $button_text; ?></a>
<?php endif; ?>
</div><!-- resource -->
<?php endwhile; ?>
</div><!-- resource-row -->
</section><!-- main-content -->
</div><!-- content -->
</div><!-- row -->
</div><!-- container -->
<?php get_footer(); ?
>
```
【问题讨论】:
-
找不到您在 if 子句中设置 $thumbnail_url 值的行。您是否忘记获取 url 并将其保存到变量中,或者您的问题中缺少某些代码?
-
II 为资源页面添加了讲师的 PHP 代码,因为我不确定如何回答您的最后一个问题。
标签: php html css wordpress dynamic