首页 > 解决方案 > 获取 woocommerce 商店页面帖子缩略图

问题描述

我对 woocommerce 有疑问。我正在使用woocommerce_before_main_content钩子添加.jumbotron页面封面。我能够显示需要出现在背景图像上的页面标题和类别等内容,但是显示的图像是磨损的,我不知道为什么但是 wordpress 会显示第一个产品图像缩略图和图像我在 woocommerce 的商店页面中选择的特色是不同的。有解决办法吗?我正在使用这段代码:

function btheme_woocommerce_before_main_content()
  {
    if( has_post_thumbnail() ):
    ?>
      <div class="jumbotron jumbotron-fluid shop-page-cover" style="background-image:url('<?php echo the_post_thumbnail_url(); ?>');">
        <!-- <div class="parallax" data-parallax-image></div> -->
          <div class="container">
            <div class="row">
              <div class="col-sm-12 col-md-12 col-lg-12 text-center">
                <h1 class=""><?php woocommerce_page_title(); ?></h1>
                <?php $categories = get_terms( array('taxonomy' => 'product_cat') ); ?>
                <?php foreach( $categories as $category ): ?>
                  <h4 class="category-name d-inline"><?php echo $category->name; ?></h4>
                <?php endforeach; ?>
              </div>
            </div>
          </div>
      </div>
    <?php endif; ?>
      <div class="container-fluid content-wrapper" id="uptheme-woocommece-wrapper">
    <?php
  }
  add_action( 'woocommerce_before_main_content', 'btheme_woocommerce_before_main_content', 10 );

标签: phpwoocommercehook-woocommerce

解决方案


首先你需要通过 id 获取帖子缩略图

<?php
$shop_page_id = wc_get_page_id('shop');

function btheme_woocommerce_before_main_content()
  {
    if( has_post_thumbnail($shop_page_id) ):
    ?>
      <div class="jumbotron jumbotron-fluid shop-page-cover" style="background-image:url('<?php echo get_the_post_thumbnail_url($shop_page_id,'full'); ?>');">
        <!-- <div class="parallax" data-parallax-image></div> -->
          <div class="container">
            <div class="row">
              <div class="col-sm-12 col-md-12 col-lg-12 text-center">
                <h1 class=""><?php woocommerce_page_title(); ?></h1>
                <?php $categories = get_terms( array('taxonomy' => 'product_cat') ); ?>
                <?php foreach( $categories as $category ): ?>
                  <h4 class="category-name d-inline"><?php echo $category->name; ?></h4>
                <?php endforeach; ?>
              </div>
            </div>
          </div>
      </div>
    <?php endif; ?>
      <div class="container-fluid content-wrapper" id="uptheme-woocommece-wrapper">
    <?php
  }
  add_action( 'woocommerce_before_main_content', 'btheme_woocommerce_before_main_content', 99 );


推荐阅读