首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过CSS加载特色图像?

通过CSS加载特色图像?
EN

Stack Overflow用户
提问于 2016-10-01 19:21:03
回答 1查看 47关注 0票数 0

我的博客当前的英雄形象(粘性帖子)是静态的,因为图像是通过CSS加载的。因此,无论哪一篇文章我写得很粘,文本都是动态的。虽然我希望我的形象是动态的,并想知道最好的方法,使特征图像加载到英雄帖子动态,与正确的风格。

我该怎么处理这个?

代码语言:javascript
复制
    <div class="hero">

                <div class="hero-wrapper">

                    <div class="article-info">

                    <!-- fetch sticky and store in $sticky variable -->
                    <?php $sticky = get_option( 'sticky_posts' ); ?>

                    <!-- create a new query and store first value of the sticky -->
                    <?php $query = new WP_Query( array( 'p' => $sticky[0] ) ); ?>

                   <?php if ( $query->have_posts() ): ?>
                      <?php while ( $query->have_posts() ) : $query->the_post(); ?>

                         <p class="topic"><a href="<?php the_permalink(); ?>"><?php the_category('&nbsp'); ?></a></p> 
                         <h1 class="hero-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>

                  <?php endwhile; ?>

                  <?php wp_reset_postdata(); ?> 

                  <?php else : ?>
                   <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
                  <?php endif; ?>

                    </div>

                </div>

         </div>

scss

代码语言:javascript
复制
.hero{
  margin: 0 0 30px 0;
  background-image: url("https://placeimg.com/470/310/people");
  background-repeat: none;
  background-size: cover;
  background-position: center center; 
  @include fill-parent;
  position: relative;
  z-index: 0;
}
EN

回答 1

Stack Overflow用户

发布于 2016-10-01 19:33:17

您可以使用.hero的内联样式来完成此操作。

代码语言:javascript
复制
<!-- fetch sticky and store in $sticky variable -->
<?php $sticky = get_option( 'sticky_posts' ); ?>
<!-- create a new query and store first value of the sticky -->            
<?php $query = new WP_Query( array( 'p' => $sticky[0]   ) ); ?>
<?php if ( $query->have_posts() ): ?>
  <?php while ( $query->have_posts() ) : $query->the_post(); ?>
    <div class="hero" style="background-image: url(<?php echo $path_to_img; ?>)">
      <div class="hero-wrapper">
        <div class="article-info">
          <p class="topic">
            <a href="<?php the_permalink(); ?>">
              <?php the_category('&nbsp'); ?>
            </a>
          </p>
          <h1 class="hero-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
        </div>
      </div>
    </div>
    <?php endwhile; ?>
      <?php wp_reset_postdata(); ?>
        <?php else : ?>
          <p>
            <?php _e( 'Sorry, no posts matched your criteria.' ); ?>
          </p>
          <?php endif; ?>

和scss

代码语言:javascript
复制
.hero{
   margin: 0 0 30px 0;
   background-repeat: none;
   background-size: cover;
   background-position: center center; 
   @include fill-parent;
   position: relative;
   z-index: 0;
 }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39810602

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档