I'm not sure if I should be posting this here or in the PHP board so please forgive me if I am using the wrong board but I am stuck on a script and out of pure desperation looking for some guidance.
I am trying to build a script that will display and organise posts in their different levels of categories. The script I am working on works perfectly except when I click on the grandchild category it shows an empty div instead of the posts associated with the child category. And this is my problem, I don't know how to make my script show posts once the grandchild is clicked.
Category structure:
Parent category (category ID is 1)
- Child category
-- Grandchild category
--- Post associated with child category
*The code contains an extra function (add random featured image from the post to parent and child category as thumbnails), I mention this in case it confuses anyone.
elseif (is_page(31))/* products */ { $taxonomyName = "category"; $catparent = 1; if ($_GET) { if (array_key_exists('cat', $_GET)) { $catparent = $_GET['cat']; }} if (empty($catparent) || !is_numeric($catparent)) { $catparent = 1; } $terms = get_terms($taxonomyName,array('parent' => $catparent,'hierarchical'=>false)); foreach($terms as $term) { $RANDfeaturedImage = $term->slug; global $wp_query; $args=array( 'orderby' => 'rand', 'posts_per_page' => 1, 'post_type' => 'post', 'hide_empty' => 0, 'tax_query' => array( array( 'taxonomy' => 'category', 'field' => 'slug', 'terms' => $RANDfeaturedImage, 'class' => 'img-block', ) ) ); $new_query = null; $new_query = new WP_Query($args); while ($new_query->have_posts()) : $new_query->the_post(); echo '<div class="boxlet"><a class="img-block" href="?cat='.$term->term_id.'">'; the_post_thumbnail('medium', array( 'class' => "img-block")); echo '</a><a class="view" href="?cat='.$term->term_id.'">'.$term->name.'</a></div>'; endwhile; wp_reset_postdata(); } }
I have looked at a number of different shopping cart and catalogue plugins but they either don't work as desired or have limitations in other ways. By doing it this way I can avoid using a plugin and all products can be loaded and controlled under posts.
I would really appreciate it if someone can assist me with this. (I can PM a link to the live example of the script if need be)