繁体   English   中英

组合Wordpress多个短代码

[英]Combining Wordpress multiple shortcodes

我创建了四个简码代码片段,以显示woocommerce产品总数,以及我的三个父类别中每个类别的产品数。

它们可以工作,但是有没有办法将4个片段(总计,苹果,橘子,梨)组合成一个片段,从而允许简码中的变量表示要显示的产品数量?

// [title-total] shortcode
function total_product_count_shortcode( ) {
    $count_posts = wp_count_posts( 'product' );
    return esc_html($count_posts->publish);
}
add_shortcode( 'title-total', 'total_product_count_shortcode' );

// [title-apples] shortcode
add_shortcode( 'title-apples', function() {
$query = new WP_Query( array(
    'tax_query' => array(
        array(
            'taxonomy' => 'product_cat',
            'field' => 'id',
            'terms' => 254, // Replace with the parent category ID
            'include_children' => true,
        ),
    ),
    'nopaging' => true,
    'fields' => 'ids',
) );

return esc_html( $query->post_count );
} );

// [title-oranges] shortcode
add_shortcode( 'title-oranges', function() {
$query = new WP_Query( array(
    'tax_query' => array(
        array(
            'taxonomy' => 'product_cat',
            'field' => 'id',
            'terms' => 256, // Replace with the parent category ID
            'include_children' => true,
        ),
    ),
    'nopaging' => true,
    'fields' => 'ids',
) );

return esc_html( $query->post_count );
} );

// [title-pears] shortcode
add_shortcode( 'title-pears', function() {
$query = new WP_Query( array(
    'tax_query' => array(
        array(
            'taxonomy' => 'product_cat',
            'field' => 'id',
            'terms' => 214, // Replace with the parent category ID
            'include_children' => true,
        ),
    ),
    'nopaging' => true,
    'fields' => 'ids',
) );

return esc_html( $query->post_count );
} );

是的,它有可能。 由于您的后三个简码非常相似(只是'terms'值的更改),我们可以将其合并为一个…然后下面的代码会将您的4个简码嵌入其中:

function custom_multi_shortcode( $atts, $content = null ) {
    // Shortcode attributes
    $atts = shortcode_atts(
        array( 'term' => '' ),
        $atts, 'cmulti'
    );

    // 1 - Total product count
    if ( empty($atts['term']) ):    
        $count_posts = wp_count_posts( 'product' );
        return esc_html($count_posts->publish);

    // 2 to 4 - Apples, Oranges and Pears
    else:
        $query = new WP_Query( array(
            'tax_query' => array(
                array(
                    'taxonomy' => 'product_cat',
                    'field' => 'id',
                    'terms' => (int) $atts['term'], // Replace with the parent category ID
                    'include_children' => true,
                ),
            ),
            'nopaging' => true,
            'fields' => 'ids',
        ) );
        return esc_html( $query->post_count );
    endif;
}
add_shortcode( 'cmulti', 'custom_multi_shortcode' );

代码进入您的活动子主题(或活动主题)的function.php文件中。 经过测试和工作。

用法(每个):

  1. 产品总数: [cmulti]
  2. 苹果: [cmulti term='254']
  3. 橘子: [cmulti term='256']
  4. 梨子: [cmulti term='214']

暂无
暂无

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

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