• Resolved lilqhgal

    (@lilqhgal)


    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 am not a programmer by any means. Does anybody have a snippet of code that would work for this? Or perhaps an article I could read more about assigning dynamic variables in the loop? Thanks!

Viewing 15 replies - 1 through 15 (of 15 total)
  • It is easy to use a variable in the query:

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

    The hard part is: How do you set the variable to the desired value? What are the critera you would use to set the variable to ‘shirts’ rather than ‘shoes’?

    Thread Starter lilqhgal

    (@lilqhgal)

    Thanks vtxyzzy –

    Well in my template I’m looping through ALL custom posts with the post_type of “product_listing” and I want to display 4 of each different ‘category’ (for lack of better words).

    I have my custom taxonomy of “product_cat” set up so the client can go into the dashboard and add ‘categories’ to her hearts content. Since this is a simple kind of online catalog, she has several products for each ‘category’ and I’m trying to code my template to display the 4 most recent posts from each one on the homepage. I can DO it, if I use the code posted in my original post. The problem then becomes that if she ads a new taxonomy in the dashboard, I have to go in and add a new loop in the index.php template file – which is FINE but I’m trying to make this automatic and instantaneous.

    So I guess to answer your question, the variable would have to be set from whatever taxonomy the client used. So if she creates a ‘category’ of “shoes”, the code would already be able to handle that without me having to add ‘product_cat’ => ‘shoes’ to my code in a new loop.

    Sounds like you will need to add an ‘outer’ loop to loop through all the ‘product_cat’ values.

    Can you put the code for the template in a pastebin and post a link to it here?

    Thread Starter lilqhgal

    (@lilqhgal)

    I don’t really have working code yet. So far, all I had was it grabbing ALL the “product_listing” posts and displaying them. She has too many products for that, otherwise there’ll be 100 posts on the page! Here’s what I had so far:

    <!-- BEGIN CODE FOR PRODUCT AREA -->
    		<div id="products">
    		<!-- post begin -->
    		<?php $loop = new WP_Query( array( 'post_type' => 'product_listing', 'posts_per_page' => 100 ) ); ?>
    		<?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>

    (I’m also using several other loops, one is in the header, one above this, to display other posts/categories and things. Here is the entire index.php file minus calls to header/footer: https://wordpress.pastebin.com/0URzVLr0 )

    And this is part of my functions.php file for the custom post_type and taxonomies: https://wordpress.pastebin.com/w3pL9WNX

    Thanks so much for taking a peek!

    I can’t really test this, but it should be close:

    <!-- 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 ?>
    Thread Starter lilqhgal

    (@lilqhgal)

    Awesome awesome AWESOME!!!

    Thanks SO MUCH!! It works! Now I just have to tweak it to add a “see more products from X category” and I’m golden! (and I can handle that)

    Thanks again for all your help!

    Thread Starter lilqhgal

    (@lilqhgal)

    If you have a WPQuestions account, I posted it on there and there are no answers yet. I could award the prize money to you if you post over there.

    Thanks for the offer, but I only do this for the fun of it.

    Thread Starter lilqhgal

    (@lilqhgal)

    Well thank you so much.

    You are most welcome!

    Thread Starter lilqhgal

    (@lilqhgal)

    vtxyzzy,

    Have another question regarding this issue. It seems that the loops cycles thru alphabetically. Is there some way to have it display a custom order? I could add some custom fields if need be, but just wondering what the best option would be. Ideally I’d like it to be the client’s options, so she could change them at her will, but if it’s something I need to set in the loop itself, she has given me a specific list so I could order it that way.

    Thanks so much again!

    Please give me a little more information. I assume you are talking about the order of the product_cat terms. The order can certainly be changed by modifying the code.

    The most ‘user friendly’ way I can think of to do that would be to add a theme option that lets the user enter a comma separated list of names in the desired order. If you are interested in doing that, the Option Tree plugin gives you an easy way to add options.

    Of course, you could always hard-code a list, but that means long-term maintenance headaches.

    Let me know how you want to proceed.

    Thread Starter lilqhgal

    (@lilqhgal)

    Thanks for the quick reply.

    Yes, the order of the product_cat terms. Currently they’re ordered the same as they are in the dashboard: alphabetically a-z. Ideally, I would love for some interface to be in place in the admin category screen that would let the user drag to reorder, or perhaps simply have a box next to the name somewhere to type in a new display order. So Shirts could be 1, Purses could be 2, and Hair Accessories could be 3. Then the front end would display that order instead.

    I checked the Option Tree plugin and it seems extremely strong but I’m not sure if I’m ready to take on a whole new learning curve right now!

    Your idea for drag-and-drop or order number would be nice, but I don’t know of any way to do that short of a big programming effort.

    I had another idea – since category descriptions are not normally seen by the viewer, what about using the category description to set the order?

    The description could be the sort order followed by the normal description text.

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘Dynamic variable for custom taxonomy in loop?’ is closed to new replies.