Horje
Show custom posts by category - but sort by subcategory - Wordpress Solution
I have made a custom post type (for my products) and a corresponding hierarchical taxonomy (a catalog for my products). I have renamed category.php to taxonomy.php. It now shows all my products that belong to a certain category. Now, since I have a lot of products, I don't want all of them listed on that page, I'd rather have them grouped by subcategory. Let me give you an example: Say I sell clothing and my taxonomy looks like this: - Pants - - Loose Fit - - Baggy Style - Shoes - - Leather - - Suede When taxonomy.php is called to show all pants, I'd like to have them listed like this: - Loose Fit - - Loose Pants #1 - - Loose Pants #2 (...) - Baggy Style - - Baggy Pants #1 - - Baggy Pants #2 (...)

Solution - 1

Hello, I think I've cracked that. It's possible to query by custom taxonomies - it's just has not been documented yet. OK, I hope I've understood the question right. Here we go. I've used the default TwentyTen theme with a custom taxonomy called "Catalog" and post type called "Products". 1) Put this in functions.php


// register custom post types and taxonomies
function register_custom_post_types() {
	
	// register custom post type
	$labels = array(
		'name' => 'Products',
		'singular_name' => 'Product'
	);
	
	$args = array(
		'labels' => $labels,
		'public' => true,
		'capability_type' => 'post',
	); 
	
	register_post_type('products', $args);
	
	// register taxonomy
	$labels = array(
		'name' => 'Catalogs',
		'singular_name' => 'Catalog',
	); 	

	register_taxonomy('catalog', array('products'), array(
		'hierarchical' => true,
		'labels' => $labels,
		'public' => true
		)
	);
	
}
add_action('init', 'register_custom_post_types');
2) Put this in your taxonomy-catalog.php

<?php
// taxonomy-catalog.php

// set variables
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
$tax = get_query_var( 'taxonomy' );
 
get_header(); ?>

		<div id="container">
			<div id="content" role="main">

				<h1 class="page-title"><?php echo $term->name; ?></h1>
				<?php
				
				// if term has children
				if (get_term_children($term->term_id, $tax) != null) {

					// get child terms
					$term_children = get_terms(
						$tax, 
						array(
							'child_of' => $term->term_id,
						)
					);
					
					// loop through them
					foreach ($term_children as $term_child) {
					
						// print title
						echo '<h2>' . $term_child->name . '</h2>';
					
						// change query
						query_posts(array(
							'catalog' => $term_child->slug,
							)
						);
						
						// loop through posts
						if (have_posts()) : ?>
						
							<ul>
							
							<?php while (have_posts()) : the_post(); ?>
								
								<li <?php post_class(); ?>>
								
									<a rel="nofollow" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
										
								</li>
								
							<?php endwhile; ?>
							
							</ul>
							
						<?php endif;
						
					}
					
				// if term has no chilren
				} else {
			
					// loop through posts
					if (have_posts()) : ?>
					
						<ul>
						
						<?php while (have_posts()) : the_post(); ?>
							
							<li <?php post_class(); ?>>
							
								<a rel="nofollow" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
									
							</li>
							
						<?php endwhile; ?>
						
						</ul>
						
					<?php endif;
			
				}
				
				?>

			</div><!-- #content -->
		</div><!-- #container -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>
3) Enjoy Please check out the result on my test site: http://test.druuf.com/wordpress/?catalogs=pants (parent category) http://test.druuf.com/wordpress/?catalogs=baggy-style (child category)


Solution - 2

Hi, You can further classified them using templates according to WordPress template hierarchy.

taxonomy-{taxonomy}-{term}.php - If the taxonomy were sometax, and taxonomy's slug were someterm WordPress would look for taxonomy-sometax-someterm.php taxonomy-{taxonomy}.php - If the taxonomy were sometax, WordPress would look for taxonomy-sometax.php
Thanks.


Solution - 3

I think you are in the situation to query posts by custom taxonomy... and that's not in WordPress, yet. You can do it with a custom select query. Something like this:


 $querystr = "
SELECT * 
FROM $wpdb->posts
LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
LEFT JOIN $wpdb->terms ON($wpdb->term_taxonomy.term_id = $wpdb->terms.term_id)
WHERE $wpdb->posts.post_type = 'post' 
AND $wpdb->posts.post_status = 'publish'
AND $wpdb->term_taxonomy.taxonomy = 'pants'
AND $wpdb->terms.slug = 'loose-fit' OR $wpdb->terms.slug = 'baggy-style'
ORDER BY $wpdb->posts.post_date DESC
";

 $pageposts = $wpdb->get_results($querystr, OBJECT);

And that is just for 2 terms... You then need to use a different loop; something like explained [[LINK href="http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query#The_Revised_Loop"]]here[[/LINK]]. Or, you can assign the default "category" taxonomy for your custom post type. Also, you won't have the custom taxonomy slug in the URL if you define your own taxonomy (something I discussed [[LINK href="http://themeforest.net/forums/thread/custom-post-typetaxonomy-permalinks/31585"]]here[[/LINK]]) Hope I understood your issue and this helps :)


Solution - 4

Its simple. even you can use your category.php file for your purpose. you just need to do setting in admin. it will display product only for selected category or subcategory whatever. Even you can also change the URL structure to your category and sub category name





Wordpress

Related
Adding Image Upload Ability To Options Panel - Wordpress Solution Adding Image Upload Ability To Options Panel - Wordpress Solution
Need to create a link in admin that uses the WP thickbox - Wordpress Solution Need to create a link in admin that uses the WP thickbox - Wordpress Solution
Formatting my Word Document into an HTML E-mail! - Wordpress Solution Formatting my Word Document into an HTML E-mail! - Wordpress Solution
Can you convert my comments form to comment_form arrays? - Wordpress Solution Can you convert my comments form to comment_form arrays? - Wordpress Solution
CSS menu issue - Wordpress Solution CSS menu issue - Wordpress Solution

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
8