通过术语ID获取术语段塞,然后分解它

时间:2017-01-25 作者:Darren Bachan

我的woocommerce产品有两个产品类别:

“11级”“12级”

我试图单独提取数字并将其输出到div中。所以我只需要“11”或“12”。

下面是我函数中的部分循环:

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);
        $url = get_permalink();

        $queried_object = get_queried_object();
        $taxonomy = $queried_object->taxonomy;
        $term_id = $queried_object->term_id; 

        $term = get_term( $id, $taxonomy );
        $slug = $term->slug;
        $explodedSlug = explode(\'-\', $slug);

        // 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">\';
        $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_query();
return $html_out;

}

add_shortcode( \'featured_courses\', \'featured_courses_query\' );
我以前从未引爆过任何东西,而且我认为我没有正确地得到子弹。我的功能肯定有问题。

1 个回复
最合适的回答,由SO网友:Anwer AR 整理而成

我想问题就在这里。

$slug = $term->slug;
$explodedSlug = explode(\'-\', $_slug);
您正在传递一个未定义的数组$_slugexplode 作用正确的数组为$slug. 因此,您的完整代码应该如下所示。

function revised_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">\';

        while ($featured_query->have_posts()) : $featured_query->the_post();

            $id = get_the_ID(); // or use $post->ID
            $terms = get_the_term( $id, \'product_cat\' );

            foreach ($terms as $term ) {
              $slug = $term->slug;
              $explodedSlugs[] = explode( \'-\', $slug );
            }
            // Your other codes goes here.


            $html_out .= \'<li class=""><div class="entry-product">\';
            $html_out .= \'<div class="course-level">\';
            foreach ($explodedSlugs as $slug ) {
              $html_out .= $slug[1];
            }
            $html_out .= \'</div>\';

            $html_out .= \'</div></li>\';

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

    endif;

    //wp_reset_query();
    return $html_out;
    }

相关推荐

如何在使用wp_set_Object_Terms时返回新添加的术语

我正在使用wp\\u set\\u object\\u terms向我们的WooCommerce产品添加术语(属性):wp_set_object_terms($id, array($migrate_case), \'pa_case\'); 这为我们提供了自动创建不存在的术语的优势(而不是使用wp\\u set\\u post\\u terms)。有没有办法让wp\\u set\\u object\\u terms()告诉我是否必须创建新的术语?提前感谢您的帮助。