通过ACF Repeater字段显示woocommerce产品的简短产品描述

时间:2018-06-20 13:47:37

标签: wordpress woocommerce advanced-custom-fields

我的代码在下面

从本质上讲,它是将woocommerce产品作为转发器字段内的发布对象发布。

除了两个例外,它几乎可以正常工作。

  1. 为$ scent变量使用get_attribute给出了错误
  2. $ description变量应提取“产品简短说明”,但不会带回任何东西。

任何帮助将不胜感激!

<?php if ( have_rows( 'products' ) ) : ?>
    <?php while ( have_rows( 'products' ) ) : the_row(); ?>

    <?php $post_object = get_sub_field( 'product' ); ?>
    <?php if ( $post_object ): ?>
        <?php $post = $post_object; ?>
        <?php setup_postdata( $post );


    global $post;

    $logoImageHtml = '';
    $subCollectionHtml = '';
    $imageAlt = '';
    $hoverImage = '';

    $args = array(
        'taxonomy' => 'product_cat',
        'orderby' => 'parent',
        'order' => 'ASC',
        'menu_order' => 'ASC',
        'pad_counts' => 0,
        'hierarchical' => 1,
        'hide_empty' => 1
    );
    $terms = wc_get_product_terms(get_the_ID(), 'product_cat', $args);
    if (!empty($terms) && is_array($terms)) {
        $terms = array_reverse($terms);

        $term = (!empty($terms[1]) ? $terms[1] : null);
        if (!empty($term->name)) {
            $imageAlt = $term->name;
        }

        $term = (!empty($terms[2]) ? $terms[2] : null);
        if (!empty($term->name)) {
            $subCollectionHtml = "{$term->name}
 ";
        }
    }

  $scent = $post->get_attribute('scent');



     $scent = (!empty($scent) ? explode(', ', $scent) : null);




    $description = get_short_description ( $post->post->ID );


    // $description = $post->post->post_excerpt;  


    $thumbImage = wp_get_attachment_image_src( get_post_thumbnail_id( $post->post->ID ), 'shop_catalog' );              

     if (get_field('product_logo_image')) {
        $logoImage = get_field('product_logo_image');
        if (!empty($logoImage)) {
            $logoImageHtml = "<h3><img class='product-logo' src='$logoImage' alt='$imageAlt'/></h3>";
        }
    }

    if (get_field('product_grid_scent_image')) {
        $hoverImage = get_field('product_grid_scent_image');
    }




                    ?>   





    <li class="product">
         <div class="front">
             <div class="image">
                 <?php if (!empty($thumbImage)): ?>
                    <img src="<?php  echo $thumbImage[0]; ?>" data-id="<?php echo $post->post->ID; ?>">
                 <?php endif; ?>
             </div>
            <?php if ($logoImageHtml || $subCollectionHtml || $scent): ?> 
                <div class="text">
                    <?php echo $logoImageHtml; ?>
                    <?php echo $subCollectionHtml; ?>
                    <?php
                    if (!empty($scent) && is_array($scent)): ?>
                        <div class="scent">
                            <ul>
                                <?php foreach ($scent as $key => $value): ?>
                                    <li><?php echo $value; ?></li>
                                <?php endforeach; ?>
                            </ul>
                        </div>
                    <?php endif; ?>
                </div>
            <?php endif; ?>
        </div>
        <div class="back">
            <div class="frame">
                <div class="content">
                    <?php if (!empty($hoverImage)): ?>
                        <div class="image">
                            <img src="<?php echo $hoverImage; ?>" alt="Scent"/>
                        </div>
                    <?php endif; ?>
                    <div class="text">
                        <?php 
                        if (!empty($description)): ?>
                            <?php echo $description; ?>ssss

                        <?php endif; ?>
                        <a href="<?php echo get_the_permalink(); ?>">View details</a>
                    </div>
                </div> 
            </div>
        </div>
    </li>





        <?php wp_reset_postdata(); ?>
    <?php endif; ?>
<?php endwhile; ?>
 <?php else : ?>
<?php // no rows found ?>
 <?php endif; ?>

0 个答案:

没有答案