2017-01-24 2 views
0

Я пытаюсь написать функцию для вывода признанных продуктов, чтобы я мог связать ее с помощью дополнительных пользовательских полей, чтобы отобразить больше данных на интерфейсе.Написание функции для вывода полезных продуктов WooCommerce

function featured_courses_query() { 
    $args = array( 
     'post_type' => 'product', 
     'meta_key' => '_featured', 
     'meta_value' => 'yes', 
     'posts_per_page' => 3 
    ); 

    $featured_query = new WP_Query($args); 

    if ($featured_query->have_posts()) : 

     $html_out = '<ul class="products">'; 

     while ($featured_query->have_posts()) : 
      $featured_query->the_post(); 
      $product = get_product($featured_query->post->ID); 

      $course_title = get_the_title($post->ID); 
      $course_level = get_field("course_level"); 
      $course_id = get_field("course_id"); 

      // Output product information here 
      $html_out .= '<li class="product type-product status-publish no-post-thumbnail first instock featured taxable shipping-taxable product-type-simple"><div class="entry-product"><div class="entry-wrap"><header class="entry-header">'; 
      $html_out .= '<h4>' . $course_title . '</h4><p>' . $course_level . " - " . $course_id . '</p>'; 
      $html_out .= '</header></div></div></li>'; 

     endwhile; 
     $html_out .= '</ul>'; 

    else : // No results 
     $html_out = "No Courses Found."; 
    endif; 

    wp_reset_query(); 
    return $html_out; 
} 

add_shortcode('featured_courses', 'featured_courses_query'); 

Я не уверен, что я делаю неправильно, но когда я использую шорткод [featured_courses] он выдает то, что в else. Лучше ли написать такую ​​настраиваемую функцию или отредактировать файл WooCommerce, который содержит их короткий код?

ответ

0

Я посмотрел, как woocommerce создала свой короткий код для вывода признанных продуктов через короткий код и после изучения других сообщений с запросами я нашел решение. Вот пример того, как я использовал его:

function featured_courses_query() { 
    $meta_query = WC()->query->get_meta_query(); 
    $tax_query = WC()->query->get_tax_query(); 
    $tax_query[] = array(
     'taxonomy' => 'product_visibility', 
     'field' => 'name', 
     'terms' => 'featured', 
     'operator' => 'IN', 
    ); 
    $args = array(
     'post_type' => 'product', 
     'stock'  => 1, 
     'showposts' => 3, 
     'orderby'  => 'rand', 
     'order'  => 'DESC', 
     'meta_query' => $meta_query, 
     'tax_query' => $tax_query 
    ); 

    $featured_query = new WP_Query($args); 

    if ($featured_query->have_posts()) : 

     $html_out = '<ul class="products x-block-grid three-up">'; 

     while ($featured_query->have_posts()) : 
      $featured_query->the_post(); 
      $product = get_product($featured_query->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); 

      // Output product information here 

      $html_out .= '<li class="product type-product status-publish no-post-thumbnail first instock featured taxable shipping-taxable product-type-simple"><div class="entry-product">'; 
      if($course_icon): 
       $html_out .= '<div class="course-icon"><img src="' . $course_icon . '" alt="' . $course_title . '"></div>'; 
      endif; 
      $html_out .= '<h4>' . $course_title . '</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_query(); 
    return $html_out; 

} 

add_shortcode('featured_courses', 'featured_courses_query'); 
0
function featured_courses_query() { 
    $html_out = ""; 
    $args = array( 
     'post_type' => 'product', 
     'meta_key' => array(
        'key' => '_featured', 
        'value' => 'yes' 
        ), 
     'posts_per_page' => 3 
    ); 

    $featured_query = new WP_Query($args); 

    if ($featured_query->have_posts()) : 

     $html_out .= '<ul class="products">'; 

     while ($featured_query->have_posts()) : 
      $featured_query->the_post(); 
      $product = get_product($featured_query->post->ID); 

      $course_title = get_the_title($post->ID); 
      $course_level = get_field("course_level",$post->ID); 
      $course_id = get_field("course_id",$post->ID); 

      // Output product information here 
      $html_out .= '<li class="product type-product status-publish no-post-thumbnail first instock featured taxable shipping-taxable product-type-simple"><div class="entry-product"><div class="entry-wrap"><header class="entry-header">'; 
      $html_out .= '<h4>' . $course_title . '</h4><p>' . $course_level . " - " . $course_id . '</p>'; 
      $html_out .= '</header></div></div></li>'; 

     endwhile; 
     $html_out .= '</ul>'; 

    else : // No results 
     $html_out = "No Courses Found."; 
    endif; 

    wp_reset_query(); 
    return $html_out; 
} 

add_shortcode('featured_courses', 'featured_courses_query'); 
Смежные вопросы