From 989abe0807c69b7de1a8d4a3f65fd58636082a50 Mon Sep 17 00:00:00 2001 From: Steve Sutton Date: Wed, 16 Dec 2015 11:16:27 -0500 Subject: [PATCH] add shortcode start with templates --- index.php | 68 ++++++++++++++++++++++++++++++++++++++++ views/list_products.php | 9 ++++++ views/product_detail.php | 2 ++ 3 files changed, 79 insertions(+) create mode 100644 views/list_products.php create mode 100644 views/product_detail.php diff --git a/index.php b/index.php index a6dfec6..21024c9 100644 --- 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 index 0000000..af95346 --- /dev/null +++ b/views/list_products.php @@ -0,0 +1,9 @@ +
+ + + +
diff --git a/views/product_detail.php b/views/product_detail.php new file mode 100644 index 0000000..2ded421 --- /dev/null +++ b/views/product_detail.php @@ -0,0 +1,2 @@ +

post_title; ?>

+ post_content; ?> -- 2.17.1