From 80e126355ef728489b6c096fab2eee514fc8c223 Mon Sep 17 00:00:00 2001 From: Steve Sutton Date: Wed, 16 Dec 2015 18:53:33 -0500 Subject: [PATCH] Output all products add category drop down --- index.php | 74 +++++++++++++++++++++++++++++++-------- views/list_categories.php | 26 +++++++++----- views/list_products.php | 18 ++++++---- 3 files changed, 88 insertions(+), 30 deletions(-) diff --git a/index.php b/index.php index 9bcea20..dc59557 100644 --- a/index.php +++ b/index.php @@ -158,7 +158,7 @@ function michsci_taxonomy_filter_post_type_request( $query ) } } } - + } add_filter( 'parse_query', 'michsci_taxonomy_filter_post_type_request' ); @@ -171,15 +171,18 @@ function michsci_shortcode($atts) extract( shortcode_atts( array( 'limit' => '10' ), $atts ) ); ob_start(); - if ( $product_id = filter_var( $_REQUEST['product'] ) ) { + echo '
'; + echo michsci_get_terms_dropdown(); + echo '
'; + echo '
'; + + if ( $product_id = filter_var( $_REQUEST['product'], FILTER_VALIDATE_INT ) ) { michsci_show_product($product_id); - - } if($cat_id = filter_var( $_REQUEST['term'])) { - michsci_show_category($cat_id); - } - else { + } else if ( $cat_id = filter_var( $_REQUEST['category'], FILTER_VALIDATE_INT ) ) { + //michsci_list_categories($cat_id); + } else if ( !$catid_id && !$product_id ) { michsci_list_products(); - michsci_list_categories(); + //michsci_list_categories(); } $output = ob_get_contents(); @@ -187,6 +190,28 @@ function michsci_shortcode($atts) return $output; } +function michsci_get_terms_dropdown() +{ + $myterms = get_terms('michsci_category', array( + 'orderby' => 'name', + 'hide_empty' => true + )); + $output =""; + return $output; +} + function michsci_list_products() { global $wpdb, $wp; @@ -216,6 +241,9 @@ function michsci_list_products() $iterator = 1; foreach ( $products as $product ) { + $productTerms = wp_get_post_terms( $product->ID, 'michsci_category', 'name' ); + $pTerm = $productTerms[0]; + $product->term = $pTerm->name; $product->end = false; if ( $iterator == $totalProducts ) { $product->end = true; @@ -225,20 +253,27 @@ function michsci_list_products() . "product=" . $product->ID; ++$iterator; } - + //echo '
' . print_r($products, true) . '
'; include 'views/list_products.php'; return; } -function michsci_list_categories() +function michsci_list_categories( $cat_id = null ) { global $wpdb, $wp; $taxonomy = 'michsci_category'; - $taxonomy_terms = get_terms( $taxonomy, array( - 'hide_empty' => false, - 'parent'=> 0) ); + $args = array( + 'hide_empty' => true, + 'pad_counts' => true + ); + if ( $cat_id ) { + $args['child_of'] = $cat_id; + } else { + $args['parent'] = 0; + } + $taxonomy_terms = get_terms( $taxonomy, $args ); + - $totalTerms = count( $taxonomy_terms); $current_url = esc_url( add_query_arg( $wp->query_string, '', home_url( $wp->request ) ) ); $iterator = 1; @@ -268,7 +303,16 @@ function michsci_show_product($prod_id) function michsci_show_category($cat_id) { global $wpdb, $wp; - $term = get_terms( $cat_id ); + $term = get_terms( + 'michsci_category', + array( + 'child_of' => $cat_id, + 'hide_empty' => true, + //'parent' => $catid_id, + //'hierarchical' => false + ) + ); + echo '
' . print_r( $term, true ) . '
'; include 'views/category_detail.php'; return; } diff --git a/views/list_categories.php b/views/list_categories.php index 3220d78..3451119 100644 --- a/views/list_categories.php +++ b/views/list_categories.php @@ -1,12 +1,22 @@ - +
- - + + +
- - + +
diff --git a/views/list_products.php b/views/list_products.php index c0d4a36..a2f31a5 100644 --- a/views/list_products.php +++ b/views/list_products.php @@ -1,14 +1,18 @@
- -
-- 2.17.1