WordPress.org

Ready to get started?Download WordPress

Forums

Custom Post Type with products by Category problem (18 posts)

  1. nataschakater
    Member
    Posted 2 months ago #

    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.

  2. silver530
    Member
    Posted 2 months ago #

    Hi, change your archive-products.php file name to taxonomy.php file name, and remove the commented text at top.

  3. nataschakater
    Member
    Posted 2 months ago #

    Hi, thanks for your reply.
    Unfortunately my product page is now redirecting to Index. Category pages too. Already tried to flush my permalinks but no luck.

  4. nataschakater
    Member
    Posted 2 months ago #

    When I set my permalinks to Default the product page is showing the products correctly but the categories also show BOTH products (the title gives the product name of the product in that category). Changing the permalinks back to Post name gives me Index again.

  5. Spyro webz
    Member
    Posted 2 months ago #

    What is your problem your single product is not displaying?

    if yes then create a file with name single-product.php and show the single product there.

    i have visit your link i think you have problem with your single product?

  6. Spyro webz
    Member
    Posted 2 months ago #

    if your categories not showing the product just create a file in your theme with the name category.php and put your code there.

  7. nataschakater
    Member
    Posted 2 months ago #

    Hi there,

    No the single is working fine. The problem is that my category pages use the Index. I just created a category.php but no luck.

  8. Spyro webz
    Member
    Posted 2 months ago #

    try to put some code in category.php

    like

    <?php $loop = new WP_Query( array( 'post_type' => 'product', 'posts_per_page' => '10', 'category_name' => single_cat_title('', false)) ); ?>
    <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
    // some content of loop goes here
    <?php endwhile; ?>

  9. nataschakater
    Member
    Posted 2 months ago #

    Yeah well... I can put some code in category.php but I can't see the category.php on the frontend so why add some code?

    - Added that code, nothing changes on frontend. Categories still using Index.

    Thanks for your help btw!

  10. Spyro webz
    Member
    Posted 2 months ago #

    try this one

    taxonomy-products.php

    hope this work for you.

  11. nataschakater
    Member
    Posted 2 months ago #

    Nope, still index.

  12. Spyro webz
    Member
    Posted 2 months ago #

    Try this one
    taxonomy-products_category.php

    mean taxonomy-your taxonomy name.php

  13. GeorgeDK
    Member
    Posted 2 months ago #

    Hi @nataschakater, cpts can be tricky, alright.

    Try registering your 'products_category' into something like 'types'.

    I may be recommending this in vain, but digging further into the documentation may solve your problem: http://codex.wordpress.org/Post_Types

  14. nataschakater
    Member
    Posted 2 months ago #

    Ok, I got something...

    I changed the rewrite in my Custom Post Type to 'productitem' and the file to taxonomy-products_category.php. Now the category page is actually showing the category page!

    But... I'm not there yet. If you go to a category page now you can see that both products are in there. There should only be one in there.

    Here is the code I used:

    <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>

    Same as my archive-products.php. It should filter them but I'm not sure which code to use.

    Thanks.

  15. nataschakater
    Member
    Posted 2 months ago #

    Hi @GeorgeDK,

    Thanks for your reply. I changed 'products_category' to 'types' but nothing changed. Still the problem of above. It's not filtering the products by category.

  16. Spyro webz
    Member
    Posted 2 months ago #

  17. nataschakater
    Member
    Posted 2 months ago #

    I copied his code and yeah it seperates the products by category but it's listed right under each other on one page. I only want the product of the active category page shown. Not all categories + products.

  18. nataschakater
    Member
    Posted 2 months ago #

    I'm working on a script myself right now but I just can't figure it out...

    What I want is: display the posts when the slug and the category title are equal.

    Here is my script so far:

    <ul>
    
    <?php
        $categoryName = str_replace(' ', '-', $category->name);
        $catName = strtolower($categoryName);
    
        $product_img = wp_get_attachment_image_src(get_field('product_image'), 'Product Image');
    
        if($category->slug == $catName) {
    ?>
    
        <li><?php the_title(); ?></li>
        <img src="<?php echo $product_img[0]; ?>" alt="" />
    
    <?php } else {
        echo "Nothing...";
    } ?>
    
    </ul>

    The script works but it only displays the latest post in that category because there is no loop. Anyone can help me finish this code? I really need to figure this out fast.

    Thanks!!

Reply

You must log in to post.

About this Topic