Custom Post Type with products by Category problem
-
Hello,
I’m working on a website where I have to add products to. I created a Custom Post Type called: Products. The thing I want is to filter these products by categories. In my sidebar you can see the Product Categories. On my main Products page all products (currently 2) are displayed.
Now the problem. When I click on one of these categories, it’s going to the right URL but it’s not displaying the product in it (both categories have one of the products in it). Instead it displays the Index.
URL: Click here >
Here is the code I have in my archive-products.php:
<?php /* Template Name: Products */ get_header(); ?> <div class="pageBox"> <div class="inner"> <div class="content"> <h2><?php the_title(); ?></h2> <ul class="products"> <?php $args = array( 'post_type' => 'products', 'posts_per_page' => 9, 'paged' => $paged, 'orderby' => 'post_date', 'order' => 'DESC', 'has_archive' => true, 'taxonomy' => 'products_category' ); $loop = new WP_Query($args); while ( $loop->have_posts() ) : $loop->the_post(); $product_img = wp_get_attachment_image_src(get_field('product_image'), 'Product Image'); ?> <li> <a href="<?php the_permalink(); ?>"> <img src="<?php echo $product_img[0]; ?>" alt="" /> </a> <a href="<?php the_permalink(); ?>"> <h2><?php the_title(); ?></h2> </a> <p><?php echo get_field('product_description'); ?></p> </li> <?php endwhile; ?> </ul> </div> <div class="sidebar"> <?php if(!function_exists("dynamic_sidebar") || !dynamic_sidebar("Sidebar Products")): ?><?php endif; ?> </div> </div> </div> <?php get_footer(); ?>
And here the CPT in my functions.php:
function products_post_type() { register_post_type( 'products', array( 'labels' => array( 'name' => __( 'Products' ), 'singular_name' => __( 'Products' ) ), 'taxonomies' => array( 'products_category' ), 'public' => true, 'show_ui' => true, 'has_archive' => true, 'hierarchical' => true, 'rewrite' => array('slug' => 'products'), 'query_var' => true, 'supports' => array( 'title', 'custom-fields', 'editor', 'thumbnail', 'taxonomies', 'category' ) ) ); register_taxonomy('products_category','Categorie', array( 'sort' => true, 'args' => array( 'orderby' => 'term_order' ), 'hierarchical' => true, 'rewrite' => array( 'slug' => 'products' ), 'labels' => $labels )); register_taxonomy_for_object_type('products_category', 'products'); } add_action( 'init', 'products_post_type' ,1);
Any help is welcome!
Thanks in advance.
- The topic ‘Custom Post Type with products by Category problem’ is closed to new replies.