2017-07-25 74 views
1

我试图让我的插件使用jQuery的滑块使用主题的特色产品阵列 我已经做了这个功能,并从class-wc-shortcodes.php 得到attrs但没有结果获取WooCommerce特色产品的自定义简码

add_shortcode('soqopslider', 'wps_soqopslider'); 
function wps_soqopslider() { 

     $atts = shortcode_atts(array(
      'per_page' => '12', 
      'columns' => '4', 
      'orderby' => 'date', 
      'order' => 'desc', 
      'category' => '', // Slugs 
      'operator' => 'IN', // Possible values are 'IN', 'NOT IN', 'AND'. 
     ), $atts, 'featured_products'); 

     $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', 
     ); 


    $query_args = array(
       'post_type'   => 'product', 
       'post_status'   => 'publish', 
       'ignore_sticky_posts' => 1, 
       'posts_per_page'  => $atts['per_page'], 
       'orderby'    => $atts['orderby'], 
       'order'    => $atts['order'], 
       'meta_query'   => $meta_query, 
       'tax_query'   => $tax_query, 
      ); 

    // The Query 
    $the_query = new WP_Query(query_args); 

    // The Loop 
    if ($the_query->have_posts()) { 
     echo '<ul>'; 
     while ($the_query->have_posts()) { 
      $the_query->the_post(); 
      echo '<li>' . get_the_title() . '</li>'; 
     } 
     echo '</ul>'; 
     /* Restore original Post Data */ 
     wp_reset_postdata(); 
    } else { 
     echo "No featured products found :("; 
    } 

    return "<span style='background:green;color:white;' >nothing</span>"; 

} 

什么,我必须补充或更改,以获得它的工作原理 我用它现在在欢迎页面短代码只是为了测试

回答

2

有在你的代码的一些错误。所以我有女佣必要的改变。

此外,短代码数据必须返回而不是回显。

下面是功能代码:

add_shortcode('soqopslider', 'wps_soqopslider'); 
function wps_soqopslider($atts) { 

    $atts = shortcode_atts(
     array(
      'per_page' => '12', 
      'columns' => '4', 
      'orderby' => 'date', 
      'order' => 'desc', 
      'category' => '', // Slugs 
      'operator' => 'IN', // Possible values are 'IN', 'NOT IN', 'AND'. 
     ), $atts, 'soqopslider' 
    ); 

    $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', 
    ); 

    $query_args = array(
     'post_type'   => 'product', 
     'post_status'   => 'publish', 
     'ignore_sticky_posts' => 1, 
     'posts_per_page'  => $atts['per_page'], 
     'orderby'    => $atts['orderby'], 
     'order'    => $atts['order'], 
     'meta_query'   => $meta_query, 
     'tax_query'   => $tax_query, 
    ); 

    // The Query 
    $the_query = new WP_Query($query_args); 

    $html = '</ul>'; 

    // The Loop 
    if ($the_query->have_posts()) { 
     while ($the_query->have_posts()) { 
      $the_query->the_post(); 
      $html .= '<li>' . get_the_title() . '</li>'; 
     } 
     // Restore original Post Data 
     wp_reset_postdata(); 
     // Output 
     return $html . '</ul>'; 
    } else { 
     return "No featured products found :("; 
    } 
} 

## BASIC USAGE: [soqopslider] 

# ---- # 

代码放在您的活动子主题(或主题)的function.php文件或也以任何插件文件。

此代码已经过测试,将输出特性产品标题列表。

相关问题