Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Dynamic variable for custom taxonomy in loop? WordPress

  • SOLVED

I have a custom page template that loops through all custom posts with the post_type of "product_listing" AND the custom taxonomy "product_cat" of "shirts" and returns 4 posts per page (as seen below:)

<?php $loop = new WP_Query( array( 'post_type' => 'product_listing', 'product_cat' => 'shirts', 'posts_per_page' => 4 ) ); ?>


It is the client's responsibility to manage those categories. I'd like to assign a variable to display in place of "shirts" so that I don't have to modify the template each time the client adds a new product category (such as shoes, pants, etc.). I would like to utilize the custom taxonomy I have created (product_cat) and not have to create a custom field.

I am not a programmer by any means. Does anybody have a snippet of code that would work for this? Thanks!

EDITED TO ADD ::::::::::::

I got my original question answered by wrapping my code in a custom loop as shown by the following code:

<!-- BEGIN CODE FOR PRODUCT AREA -->
<?php $prod_cats = get_terms('product_cat');
foreach ($prod_cats as $prod_cat) {
$cat_name = $prod_cat->name; ?>
<div id="products">
<!-- post begin -->
<?php $loop = new WP_Query( array( 'post_type' => 'product_listing', 'posts_per_page' => 4, 'product_cat' => $cat_name ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="product-tease" id="post-<?php the_ID(); ?>">
<div class="upper">
<h3><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>
<p align="center"><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><img src="<?php echo catch_that_image() ?>" /></a></p>
<?php the_excerpt('Read the rest of this entry &raquo;'); ?>
</div>
<span class="btn-readon"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">Read On</a></span>
</div>
<?php endwhile; ?>
<br clear="all" />
<!-- post end -->
<br clear="all" />
<?php wp_reset_query(); ?>
<?php rewind_posts(); ?>
</div>
<?php } // End foreach $prod_cats ?>


However, now I'm having fits with my custom taxonomy and permalinks, as seen here:
http://wordpress.stackexchange.com/questions/5308/custom-post-types-taxonomies-and-permalinks

Answers (2)

2010-12-13

Michael Fields answers:

Not sure that you need to be using a page template for this at all. You should be able to get away with using the taxonomy archive templates in WordPress 3.0. I have a plugin which you can use to display navigation to these pages in any of your theme's sidebars. The plugin can be found here: [[LINK href="http://wordpress.org/extend/plugins/taxonomy-widget/"]]http://wordpress.org/extend/plugins/taxonomy-widget/[[/LINK]]

You can read more about how the custom taxonomy template hierarchy works here:
[[LINK href="http://codex.wordpress.org/Template_Hierarchy#Custom_Taxonomies_display"]]http://codex.wordpress.org/Template_Hierarchy#Custom_Taxonomies_display[[/LINK]]

After you are using the archive pages, you can then modify the loop in your custom page template by removing the 'product_cat' argument and use it to display all products, or remove it entirely if there is no need to show all products.

Best wishes,
-Mike

2010-12-14

Peter Michael answers:

You could use custom rewrite rules for this. Add to functions.php, replace YOUR_PAGENAME with the page slug. You may have to alter the rule as I don't know your page nav structure.


# Add custom rules to wp_rewrite
function fd_add_rewrite_rules( $wp_rewrite )
{
$new_rules = array
(
'YOUR_PAGENAME/([^/]+)/?$' => 'index.php?pagename=YOUR_PAGENAME&product_cat='.$wp_rewrite->preg_index(1)
);
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
# Query Vars used by rewrite rules
function fd_insert_rewrite_query_vars($public_query_vars)
{
$public_query_vars[] = "product_cat";
return $public_query_vars;
}
add_action('generate_rewrite_rules', 'fd_add_rewrite_rules', 1);
add_filter('query_vars','fd_insert_rewrite_query_vars', 1);


Go to WP Admin -> Setting -> Permalinks and update the permalink structrue. You should now be able to access the var via $_GET['product_cat'] (i.e. 'shirts') at http://yoursite.com/YOUR_PAGENAME/shirts