add shortcode start with templates
authorSteve Sutton <steve@gaslightmedia.com>
Wed, 16 Dec 2015 16:16:27 +0000 (11:16 -0500)
committerSteve Sutton <steve@gaslightmedia.com>
Wed, 16 Dec 2015 16:16:27 +0000 (11:16 -0500)
index.php
views/list_products.php [new file with mode: 0644]
views/product_detail.php [new file with mode: 0644]

index a6dfec6..21024c9 100644 (file)
--- a/index.php
+++ b/index.php
@@ -164,3 +164,71 @@ add_filter( 'parse_query', 'michsci_taxonomy_filter_post_type_request' );
 
 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' );
diff --git a/views/list_products.php b/views/list_products.php
new file mode 100644 (file)
index 0000000..af95346
--- /dev/null
@@ -0,0 +1,9 @@
+<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>
diff --git a/views/product_detail.php b/views/product_detail.php
new file mode 100644 (file)
index 0000000..2ded421
--- /dev/null
@@ -0,0 +1,2 @@
+<h2><?php echo $product->post_title; ?></h2>
+    <?php echo $product->post_content; ?>