add_action( 'init', 'michsci_taxonomies' );
add_action( 'init', 'michsci_custom_post_type' );
+
+function michsci_shortcode($atts)
+{
+ extract( shortcode_atts( array( 'limit' => '10' ), $atts ) );
+ ob_start();
+
+ if ( $product_id = filter_var( $_REQUEST['product'] ) ) {
+ michsci_show_product($product_id);
+ } else {
+ michsci_list_products();
+ }
+
+ $output = ob_get_contents();
+ ob_end_clean();
+ return $output;
+}
+
+function michsci_list_products()
+{
+ global $wpdb, $wp;
+ $args = array(
+ 'post_per_page' => -1,
+ 'post_type' => 'miproduct'
+ );
+
+ $misch_category = ( isset( $_REQUEST['michsci_category'] ) )
+ ? filter_var( $_REQUEST['michsci_category'] )
+ : false;
+
+ if ($misch_category) {
+ $args['tax_query'] = array(
+ 'relation' => 'AND'
+ );
+ $args['tax_query'][] = array(
+ 'taxonomy' => 'michsci_category',
+ 'field' => 'id',
+ 'terms' => $misch_category
+ );
+ }
+
+ $products = get_posts( $args );
+ $totalProducts = count( $products );
+ $current_url = esc_url( add_query_arg( $wp->query_string, '', home_url( $wp->request ) ) );
+ $iterator = 1;
+
+ foreach ( $products as $product ) {
+ $product->end = false;
+ if ( $iterator == $totalProducts ) {
+ $product->end = true;
+ }
+ $product->href = $current_url
+ . ( ( strpos( $current_url, '?' ) ) ? '&' : '?' )
+ . "product=" . $product->ID;
+ ++$iterator;
+ }
+
+ include 'views/list_products.php';
+ return;
+}
+
+function michsci_show_product($prod_id)
+{
+ global $wpdb, $wp;
+ $product = get_post( $prod_id );
+ include 'views/product_detail.php';
+ return;
+}
+add_shortcode( 'michproducts', 'michsci_shortcode' );
--- /dev/null
+<div class="row">
+ <?php if ( isset( $products ) && is_array( $products ) ) : foreach ( $products as $product ) : ?>
+ <div class="small-12 medium-3 columns<?php if ($product->end){ echo ' end';}?>">
+ <a href="<?php echo $product->href; ?>">
+ <?php echo $product->post_title; ?>
+ </a>
+ </div>
+ <?php endforeach; endif; ?>
+</div>