我想使用woocommerce短代码来输出某个类别的产品,但是会从高级自定义字段创建的自定义字段中添加更多数据。以下是我使用
的代码function category_products( $atts ) {
// global $woocommerce_loop;
extract(shortcode_atts(array(
'category' => '',
'per_page' => '4',
'columns' => '4',
'orderby' => 'date',
'order' => 'desc'
), $atts));
$meta_query = WC()->query->get_meta_query();
$tax_query = WC()->query->get_tax_query();
$tax_query[] = array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( esc_attr($category) ),
'operator' => 'IN',
);
$meta_query[] = array(
'key' => '_visibility',
'value' => array('catalog', 'visible'),
'operator' => 'IN',
);
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => $per_page,
'orderby' => $orderby,
'order' => $order,
// 'stock' => 1,
// 'showposts' => 3,
// 'orderby' => 'rand',
// 'order' => 'DESC',
'meta_query' => $meta_query,
'tax_query' => $tax_query
);
$products = new WP_Query( $args );
$html_out = '';
// $woocommerce_loop['columns'] = $columns;
if ( $products->have_posts() ) :
$html_out .= '<ul>';
while ($products->have_posts()) :
$products->the_post();
$product = get_product( $products->post->ID );
$course_title = get_the_title($post->ID);
$course_level = get_field( "course_level" );
$course_id = get_field( "course_id" );
$course_icon = get_field( "course_icon" );
$excerpt = get_the_excerpt($post->ID);
$url = get_permalink();
$term_list = wp_get_post_terms($products->post->ID, 'product_cat', array("fields" => "all"));
$first_term = $term_list[0];
$first_term_slug = $first_term->slug;
$explodedSlug = explode('-', $first_term_slug);
// Output product information here
$html_out .= '<li class="' . $first_term_slug . 'product type-product status-publish no-post-thumbnail first instock featured taxable shipping-taxable product-type-simple"><div class="entry-product">';
$html_out .= '<div class="course-level">' . $explodedSlug[1] . '</div>';
if( $course_icon ):
$html_out .= '<div class="course-icon"><img src="' . $course_icon . '" alt="' . $course_title . '"></div>';
endif;
$html_out .= '<h4><a href="' . $url . '">' . $course_title . '</a></h4><p>' . $course_level . " - " . $course_id . '</p><p>' . $excerpt . '</p>';
$html_out .= '</div></li>';
endwhile;
$html_out .= '</ul>';
else : // No results
$html_out = "No Courses Found.";
endif;
wp_reset_postdata();
return $html_out;
}
add_shortcode(&#39; category_products&#39;,&#39; category_products&#39;);
我尝试使用短代码[category_products category="grade-11"]
,但只输出else
。