0

I have two product categories for my woocommerce product:

"grade-11" "grade-12"

I'm trying to extract the numbers alone and output them in a div. So I only need "11" or "12".

Here's part of the loop in my function:

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

I've never exploded anything before, that and I don't think I'm getting the slug properly. Something is definitely wrong in my function.

1 Answer 1

1

I guess the problem is here.

$slug = $term->slug;
$explodedSlug = explode('-', $_slug);

you are passing an undefined array $_slug to explode function. the correct array is $slug. so your complete code should be something like below.

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;
    }
5
  • ah I fixed that, but it still didn't produce anything, var_dump still shows Null. Jan 25, 2017 at 18:29
  • @Darren Bachan can you please paste the complete code here so we will get more accurate idea?
    – Anwer AR
    Jan 25, 2017 at 18:44
  • updated the OP to the full function. Basically what I'm trying to do is pull the ID of the product category or slug and extract the number from it so if its "grade-11" I need "11. Then I need to output that into a div. Jan 25, 2017 at 18:48
  • @DarrenBachan try the new updated code.
    – Anwer AR
    Jan 25, 2017 at 19:09
  • I had to nix $id and $terms and rewrite it as $terms = get_the_terms( get_the_ID(), 'product_cat' ); in order for it to work. Thanks! Feb 14, 2017 at 1:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.