如何按类别过滤特色产品滑块?

时间:2014-01-13 16:04:37

标签: php woocommerce

我正在尝试在WooCommerce商店的类别页面上显示“特色产品滑块”,这样只会在滑块中显示当前类别(或特定子类别)的“特色”产品。 我把下面的代码放在了我的child-theme的functions.php文件中。 我尝试在archive.php中添加这个条件:

   if(is_product_category('slug'){echo featured_products_slider_fun($atts, $content);}

我无法弄清楚如何将这种情况与下面的功能结合起来,基本上显示某个类别的“特色产品”。

===========

function featured_products_slider_func($ atts,$ content){     extract(shortcode_atts(array(         'num'=> 4     ),$ atts));

$return = '';

$first_product_title = "";
$first_product_excerpt = "";

function get_the_popular_excerpt(){
    $excerpt = get_the_content();
    $excerpt = preg_replace(" (\[.*?\])",'',$excerpt);
    $excerpt = strip_shortcodes($excerpt);
    $excerpt = strip_tags($excerpt);
    $excerpt = substr($excerpt, 0, 190);
    $excerpt = substr($excerpt, 0, strripos($excerpt, " "));
    $excerpt = trim(preg_replace( '/\s+/', ' ', $excerpt));
    $excerpt = $excerpt.'. <a href="'.get_permalink().'">view product</a>';

    return $excerpt;
}

$post = new WP_Query(array(
'post_status' => 'publish',
    'post_type' => 'product',
    'meta_key' => '_featured',
    'meta_value' => 'yes',
    'posts_per_page' => $num
));

$return .= '<div class="featured-slider-outer box clearfix"><div class="featured-slider-inner">';
    $return .= '<ul class="products featured-slider">';
    $selected_class = " selected";

    if ($post ->have_posts()) : while ($post ->have_posts()) : $post ->the_post();

        global $product;

        if( $selected_class != "" ) {
            $first_product_title = get_the_title();
            $first_product_excerpt = get_the_popular_excerpt();
        }

        $return .= '<li class="product' . $selected_class . '">';

            $return .= get_the_post_thumbnail($post->ID, "blog-two-column");

            if ($price_html = $product->get_price_html()) :
                $return .= '<span class="price">';
                    $return .= $price_html;
                $return .= '</span>';
            endif;

            $return .= "<div class='none'>";
                $return .= "<h3 id='infos-title'>" . get_the_title() . "</h3>";
                $return .=  "<div id='infos-excerpt'>" . get_the_popular_excerpt() . "</div>";
            $return .= "</div>";
        $return .= '</li>';

        $selected_class = "";

    endwhile;

    $return .= '</ul>';
$return .= '</div>';

$return .= '<div class="featured-slider-right">';
    $return .= '<button class="btn-left"></button><button class="btn-right"></button><h2>' . $first_product_title . '</h2>';
    $return .= '<div class="description">' . $first_product_excerpt . '</div>';
$return .= '</div>';

$return .= '</div>';

else:
    $return = __("No featured products found!", "shopifiq");
endif;

return $return;

}

add_shortcode('featured_products_slider','featured_products_slider_func');

0 个答案:

没有答案