WordPress.org

Ready to get started?Download WordPress

Forums

Custom Taxonomy and Category Problems (4 posts)

  1. whiteorange39
    Member
    Posted 6 months ago #

    Hello!

    Bit green around the edges in php, but I've been currently tasked with fixing my companies old website design (not coded by me) and its been a nightmare.

    I have one problem that is particularly tough for me to tackle and that is creating a product page using categories and a custom taxonomy. This was all previously set up but currently looks a bit odd as it displays every product in every category without listing it according to the category hierarchy set up in the front end.

    For example:

    The page displays as such-

    Wardrobes
    -Item A
    -Item B
    -Item C
    - etc.

    Modern
    -Item A
    -Item B
    -Item C
    - etc.

    Classic
    -Item A
    -Item B
    -Item C
    - etc.

    I'd like it list as such:

    Wardrobes
    -Modern
    --Item A
    --Item B
    --Item C
    -- etc.
    -Classic
    --Item A
    --Item B
    --Item C
    -- etc.

    This is how it is set up through the categories in the front end, but I cant quite understand how I'm calling this up wrong in my php.

    This is what I have:

    <div id="primary" class="site-content">
    		<div id="content" role="main">
    			<?php $terms = get_terms('productcat');
    
    			    $count = count($terms);
    				if ($count > 0) {
    			     foreach ( $terms as $term) {?>
    				<div class="products-grid">
    
    				<h3><?php echo $term->name; ?></h3>
    					<div class="wrap">
    			        <?php $loop = new WP_Query(array('productcat' => $term->slug, 'posts_per_page' => 999) );
    							$counter = 999;
    			            while ( $loop->have_posts() ) :?>
    
    			 			<?php $loop->the_post(); ?>
    			            <div class="product">

    Any pointers would really help. Thanks.

  2. bcworkz
    Member
    Posted 5 months ago #

    WP_Query is unable to parse custom taxonomy slugs, you need to use 'tax_query' with custom taxonomies. This also allows for more complex taxonomy queries and accepts categories and tags as taxonomies, so you can specify how you want categories to relate to your custom taxonomy here.

    Your code is not referencing categories at all. Were you saving this for later? Or were you considering your taxonomy as a category? There seems to be an element missing here.

  3. whiteorange39
    Member
    Posted 5 months ago #

    Hey thanks for the response.

    I really wish I was able to explain the dynamics of the structure these codes were built on, but the original files were not organized by me. I am simply trying to break them down and reorganize. So that my archive actually displays itself in a hierarchy. Would it help to see the entire code setup on the page? The above was just a snippet from the php file.

    Here is the entire file if it makes more sense-

    <?php
    
    get_header(); ?>
    
    	<div id="primary" class="site-content">
    		<div id="content" role="main">
    
    			<?php $terms = get_terms('productcat');
    
    			    $count = count($terms);
    				if ($count > 0) {
    			     foreach ( $terms as $term) {?>
    				<div class="products-grid">
    
    				<h3><?php echo $term->name; ?></h3>   
    
    					<div class="wrap">
    			        <?php $loop = new WP_Query(array('productcat' => $term->slug, 'posts_per_page' => 999) );
    							$counter = 999;
    			            while ( $loop->have_posts() ) :?>
    
    			 			<?php $loop->the_post(); ?>
    			            <div class="product">
    							<a href="javascript:;" rel="<?php echo get_the_id(); ?>" class="postpopup">
    							<div class="product-thumbnail">
    								<?php if (has_post_thumbnail( $post->ID ) ): ?>
    								<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
    
    								<a href="#<?php echo $post->ID ?>" rel="lightbox" data-ob="lightbox[products]" ><?php the_post_thumbnail(); ?></a>
    
    								<div id="<?php echo $post->ID ?>" style="display:none">
    									<div id="product-image"><?php the_post_thumbnail(); ?></div>
    									<div id="product-info">
    										<h3><?php echo $term->name; ?><span class="product-title"><?php the_title(); ?></span></h3>
    
    										<?php the_content(); ?>
    
    										<div id="product-specs">
    										<p><strong>Bredd</strong>
    										<?php the_field('product_width'); if(get_field('product_width', $post->ID)) echo(" cm"); ?></p>
    										<p><strong>Höjd</strong> <?php the_field('product_height'); if(get_field('product_height', $post->ID)) echo(" cm"); ?></p>
    										<p><strong>Djup</strong> <?php the_field('product_depth'); if(get_field('product_depth', $post->ID)) echo(" cm"); ?></p>
    										<br>
    										<p><?php if(get_field('product_doortype', $post->ID)) echo("<strong>Dörrtyp </strong>"); the_field('product_doortype'); ?></p>
    										<p><?php if(get_field('product_cabinet', $post->ID)) echo("<strong>Kabinettyp </strong>"); the_field('product_cabinet'); ?></p>
    										<p><?php if(get_field('product_price', $post->ID)) echo("<strong>Pris </strong>"); the_field('product_price'); ?></p>
    
    										</div>
    
    									</div>
    
    								</div>
    
    								<?php endif; ?>
    							</div>
    							<span class="product-details">
    								<?php the_title(); ?> <br />
    								Art nr <?php the_field('product_nr'); ?>
    							</span>
    							</a>
    						</div>
    			            <?php endwhile; ?>
    						</div>
    					</div>
    			    <?php }
    				}
    				?>
    
    			</div> 
    
    		</div><!-- #content -->
    	</div><!-- #primary -->
    
    <?php get_footer(); ?>

    The custom taxonomy its calling here is "productcat" which looks to be built using some plugin called "Bones" that was dropped in.

    Also- as a side note, I tried replacing WP_Query with tax_query and just received a fatal error. :
    (Fatal error: Class 'tax_query' not found in /storage/content/30/138030/modd.nu/public_html/wp-content/themes/modd-theme/archive-products.php on line 33)

  4. bcworkz
    Member
    Posted 5 months ago #

    I'm sorry I wasn't more clear. I didn't want you to replace WP_Query with tax_query, 'tax_query' is the new way to pass your taxonomy arguments to WP_Query. It is part of WP_Query, not a replacement. Using the taxonomy slug as an argument was deprecated with WP 3.1.

    Passing arguments using 'tax_query' involves nested arrays to achieve the proper structure. Examine the various examples with the link in my first post for proper array nesting.

    I don't see any use of categories in your template, it's probably just something I was expecting based on your title and a misunderstanding on my part. It appears everything works properly with your custom taxonomy, so I'm just confused, my apologies.

Reply

You must log in to post.

About this Topic