繁体   English   中英

使用自定义短代码获取 WooCommerce 特色产品

[英]Get WooCommerce featured products with a custom shortcode

我试图让一系列特色产品在我自己的插件中使用 jquery 滑块主题我已经创建了这个功能并从 class-wc-shortcodes.php 获取 attrs 但没有结果

    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>";

}

我必须添加或更改的内容才能使其正常工作我现在将其用作欢迎页面中的简码,仅用于测试

您的代码中有一些错误。 所以我做了必要的改变。

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

这是功能代码:

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 文件或任何插件文件中。

此代码经过测试,将输出功能产品标题列表。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM