duanshanduo3363 2015-08-21 11:31
浏览 64
已采纳

价格没有显示在相关产品列表中来自WooCommerce的常规代码

What I'm trying to do is show the price besides the title of the product in a related products list that is in the sidebar... but it doesn't work for some reason.

Yes, I have searched the StackOverflow archive and Google and found the obvious answer:

<?php echo $product->get_price_html(); ?>

but this one doesn't work in my code structure:

<h4>Related products</h4>
   <ul class="prods-list">
     <?php while ( $products->have_posts() ) : $products->the_post(); ?>
        <li>
          <a href="<?php the_permalink(); ?>" target="_blank">
           <?php do_action( 'woocommerce_before_shop_loop_item_title' ); ?>
           <span><?php the_title(); ?></span>
          /a>
          </li>
          <?php endwhile; wp_reset_postdata(); ?>
  </ul>

What am I doing wrong here? I tried adding that code after the title span, but it doesn't return anything.

  • 写回答

1条回答 默认 最新

  • doufan9805 2015-08-21 17:39
    关注

    It looks like you are trying to invoke a method from the Product class on a Post object. I can't tell for sure without seeing code before it, but it looks like the $products variable is set to an instance of WP_Query(). If that's the case, then you need to do two things:

    1. Get an instance of a Product object using the current Post ID
    2. Invoke the get_price_html() method on the Product object

    You can write this more succinctly, but I'm going to lay it out piece by piece to explain what each thing does. Your code should look something like this:

    <h4>Related products</h4>
      <ul class="prods-list">
        <?php while ( $products->have_posts() ) : $products->the_post(); ?>
          <li>
            <a href="<?php the_permalink(); ?>" target="_blank">
              <?php do_action( 'woocommerce_before_shop_loop_item_title' ); ?>
              <?php
    
                // Get the ID for the post object currently in context
                $this_post_id = get_the_ID();
    
                // Get an instance of the Product object for the product with this post ID
                $product = wc_get_product($this_post_id);
    
                // Get the price of this product - here is where you can 
                // finally use the function you were trying
                $price = $product->get_price_html();
    
              ?>
              <span><?php the_title(); ?></span>
    
              <?php
                // Now you can finally echo the price somewhere in your HTML:
                echo $price;
              ?>
    
            </a>
          </li>
        <?php endwhile; wp_reset_postdata(); ?>
      </ul>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上