Example: Shortcode for WooCommerce Category Variable Item

function resources_query( $atts ) {
    extract(shortcode_atts(array(
        'category'      => '',
        'per_page'  => -1,
        'orderby' => 'date',
        'order' => 'ASC'
    ), $atts));

    $tax_query = array(
        'taxonomy' => 'product_cat',
        'field'    => 'slug',
        'terms'    => array( esc_attr($category) ),
        'operator' => 'IN'
    );
	
    // Show only product variation if has the sales price 
    $meta_query = array(
	'key' => '_sale_price'
    );
	
    $args = array(
        'post_type'     => 'product_variation',
        'post_status'       => 'publish',
        'posts_per_page' => $per_page,
        'orderby' => $orderby,
        'order' => $order,
        'tax_query' => array($tax_query),
		'meta_query' => array($meta_query)
    );
    $resources_query = new WP_Query( $args );
    if ( $resources_query->have_posts() ) :
        $html_out = '<div class="fg-row row flex-row">';
        while ( $resources_query->have_posts() ) :
            $resources_query->the_post();
            $title = get_the_title();
            $content = get_the_excerpt();
            //$pdf = get_field( "download_pdf" );
			$pdf = 'Tamal';

            // Do stuff with each post here
            $html_out .= '<div>';
            $html_out .= '<h5>' . $title . '</h5><p class="icon-box-v3-title-paragraph fg-text-dark ffb-description-3">' . $content . '</p>';

            if( $pdf ):
                $html_out .= '<a href="' . $pdf . '" target="_blank"><span class="btn-text">Download PDF</span></a>';
            endif;

            $html_out .= '</div>';
        endwhile;
        $html_out .= '</div>';

    else : // No results
        $html_out = "No Resources Found.";
    endif;
    wp_reset_query();
    return $html_out;
}

add_shortcode( 'show_resources', 'resources_query' );

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>