• So I am attempting to build the first page in my navigation heirarchy to choose product categories.

    I have a custom taxonomy called collections. I would like to create a page that loops through all terms in the collections taxonomy and displays the Term +link and description.

    I created a file called taxonomy-collections.php and put the following code in. But it is not doing the trick.

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post();
    
    $terms = get_terms( 'collections' );
    
    echo '<ul>';
    
    foreach ( $terms as $term ) {
    
        // The $term is an object, so we don't need to specify the $taxonomy.
        $term_link = get_term_link( $term );
    
        // If there was an error, continue to the next term.
        if ( is_wp_error( $term_link ) ) {
            continue;
        }
    
        // We successfully got a link. Print it out.
        echo '<li><a href="' . esc_url( $term_link ) . '">' . $term->name . '</a></li>';
        echo  term_description($post->ID,$term);
    
        }
    
    echo '</ul>';
    
    ?>
    
    <?php endwhile; else : ?>
    	<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
    <?php endif; ?>

    This is almost straight from the codex so I’m guessing I am missing something.

    currently the code has each term displayed as a list, but I do want to change it to a grid format if someone could help with that too.

    So each term and description will be wrapped in a div and right aligned with the next.

    Thanks

Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator bcworkz

    (@bcworkz)

    Sorry, I’m not clear on what you are really trying to do here. A template file named taxonomy-collections.php in your theme folder (child theme I hope) is for showing posts assigned terms from the collections taxonomy. If I’m not mistaken, you just want to list all terms (with links) of that taxonomy, yes? And those term links lead to posts with that term, right?

    Or are you trying to list the terms assigned to each post? This is closer to what your code looks like, though it is wrong, as you know.

    To simply list all terms in a taxonomy, I would create a custom page template with the term loop code. Then create a page based on this template. No content is required, just assign a title so WP can display the page on request. The links would then lead to posts with that term, where the taxonomy-collections.php template would be used to display the posts if it exists, or taxonomy.php or archive.php templates would be used if not.

    As for making a grid format, all the terms output should each be in their own div. There’s any number of ways to use CSS to grid separate divs if they are the same size. If they vary in size, the jQuery Masonry plugin does a good job of making order out of various element sizes.

    Thread Starter jcc5018

    (@jcc5018)

    thanks for the reply. I actually got that part to work. I did have to create a page template. Trying to google what I wanted to accomplish was a pain in the butt.

    Anyway, next order of business is getting the actual post to display when clicked on in the archive.

    I am using PODS to create my CPT’s and taxonomies if that makes a difference.

    I’m guessing it is a rewrite issue, but whatever it is, the permalinks are not working correctly. I need them to display as “Domain/collection/%collection%/%post_title/” where %collection% is the term of the collection taxonomy and %post_title% is the post related to the term

    In the pods rewrite form, I have collection/%collection% set thinking that the %collection% would display the actual collection selected (it doesn’t).

    I also have the archive page rewrite set to the same thing, but that didnt work either. I also tried just having collection itself.

    In addition, the breadcrumbs links for individual products are not displaying the right hierarchy either. It is displaying “Home>product group>(Item) opposed to Home>Collections>%collection%>%product%

    Here is the code so far:

    page-collection.php – WORKS!

    <?php
    /*
    Template Name: Collections
    */
    get_header();
    $terms = get_terms( 'collection' );
    
    echo '<ul>';
    
    foreach ( $terms as $term ) {
    
    // The $term is an object, so we don't need to specify the $taxonomy.
    $term_link = get_term_link( $term );
    
    // If there was an error, continue to the next term.
    if ( is_wp_error( $term_link ) ) {
        continue;
    }
    
    // We successfully got a link. Print it out.
    echo '<li><a href="' . esc_url( $term_link ) . '">' . $term->name . '</a></li>';
    echo  $term->description; // This will return the description of the term
    
    }
    
    echo '</ul>';
    
    ?><?php get_footer(); ?>

    taxonomy-collection.php — Works from collections, but not from product going up.

    <?php get_header(); ?>
    
      <?php $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); ?>
      <div id="container">
        <div id="content" >
    
          <h1 class="page-title"><?php echo $term->name; ?> </h1>
    
          <?php if (have_posts()) : ?>
            <?php while (have_posts()) : the_post(); ?>
    
    /*Create filter to only display top level product groups if variants exist in any child post type relationship*/
    
              <div >
                <h2 class="entry-title">
                  <a href="<?php echo get_permalink(); ?>" title="<?php the_title(); ?>" rel="bookmark">
                    <?php the_title(); ?>
                  </a>
                </h2>
    
                          <div class="entry-summary">
                  <?php the_excerpt(); ?>
                </div><!-- .entry-summary -->
              </div>
    
            <?php endwhile; ?>
          <?php endif; ?>
    
        </div><!-- #content -->
      </div><!-- #container -->
    
    <?php get_footer(); ?>

    single-pods_product_grps.php — the main part works, but not my custom functions that display product variants

    <?php
    
    get_header();
    
    if ( have_posts() ) {
    	while ( have_posts() ) {
    		the_post();
    		if ( has_post_thumbnail() ) {
    	$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'large' );
    
             echo '<div><a href="' . $large_image_url[0] . '" title="' . the_title_attribute( 'echo=0' ) . '">';
    	the_post_thumbnail( 'medium' );
    	echo '</a></div>';
    }
    ?><div>
        <?php
        echo "Test if this is working";
    
        the_content();?> </div> <div>
            <?php
            //determine if child groups exist
            /*If product group has child groups
             * Loop child groups into drop down selection box :returns group_ID
    If no child group: just return parents group ID
             */
    
            do_action('display_products');
            echo '</div>';
    
    } // end while
    } // end if
    ?>
    <?php get_footer(); ?>
    Moderator bcworkz

    (@bcworkz)

    You figured it out from google? That’s impressive! ??

    I don’t know much about PODS, so I’m inclined to try some rewrite rules directly through the native Rewrite API, since the PODS rewrite doesn’t seem like it’s working for you. Since you have a fixed base link fragment (/collections/) the needed regexp should be straight forward.

    I think your issue is the permalink you are trying to use is doubly redundant. Meaning you could get a post just from /collections/term/ or just from post title. When the two are combined, WP seems to get confused and throws 404 errors. (I’ve been here before) The odd thing is your permalink could be domain.com/gobbledygoop/post-title and the post is returned without issue. It’s when the permalink contains terms that WP understands in addition to the title that the confusion begins. I’m afraid you’re up against something difficult to resolve. At least I haven’t had any luck with such schemes. Are you sure you need a redundant scheme? A non-redundant permalink scheme will work much better.

    In a similar manner, breadcrumbs are problematic when there is more than one way to get to a page and you want the breadcrumb to reflect the actual path taken. The code would need to build a path based on referrers instead of the taxonomy hierarchy. I’ve never attempted referrer path code, I’m not sure I’d want to, it seems problematic.

    I hate to suggest a particular scheme be used because it’s easier to code, but the schemes you’ve chosen are problematic. If it’s not super important they be that way, consider revising your schemes.

    A lot to say I can’t help you, sorry. You can wait and see if someone else jumps in here, but I think it’s unlikely. Since the initial question was resolved, consider marking this topic as resolved and starting a new topic. Keep your question focused on one particular issue, asking multi-part questions reduces the likelihood of someone helping you because multiple parts require too much brain power and people not having all the answers are less likely to chime in.

    If no one bites on one question in a reasonable time, go ahead and move on to the next question in yet another new topic. That’s the best strategy for getting some kind of answer. Good luck!

    Thread Starter jcc5018

    (@jcc5018)

    well, no, google actually wasnt much help. I ended up getting help from stack exchange, but I did get my initial code from the wordpress codex.

    anyway, there is a chance that I am not explaining myself the best. There is really only one way to get to a product.

    I have my products arranged in collections, each collection (taxonomy) has many products (product groups), and each product has many variants (sizes)

    So an 11×14 glossy print (variant) will be in the Print/ Glossy (product group/subgroup), which is in the “Looking Good collection” which is in the Collection Index. so the url would be domain/collection/looking-good/Prints/Glossy

    Variants do not have their own page, because they will be displayed in a list on the Glossy page.

    The product description and photos are all in the (Product group) cpt, but both Product-group and product variant have data needed to calculate the final price, and some variants may have additional descriptions in which case a pop up box displaying that content will appear.

    I have a demo site built in a wireframe software that explains what I need to do. Im working on the products folder right now.

    https://rxajhx.axshare.com

    you will need a password: demosite to access it.

    Thread Starter jcc5018

    (@jcc5018)

    I’m starting to wonder if I should just include the taxonomy terms as parents to the product groups in the customer post type opposed to a taxonomy. I would just need a different template for each level of the hierarchy. Not sure if that’s possible either.

    something along the lines of: (this is just a very rough outline that I’m making up as I go. I’m not worried bout correct syntax or function names at this point. If it makes sense, maybe we can go with this with the correct functions defined. Note I am using SQL, cause it makes a little more sense to me than the wordpress loop at this point.

    Create post selection box that has values (collection, product, variant)
    Each collection must have at least one product and each product must have one variant for data to be displayed.

    function get_collection ()
    {$product=select * from Product where type='collection'
    IF (count(get_product())>=1)
    {echo "Post_title";
    content();
    }
    
    function (get_product)
    {$id=get_parent_id();
    $products[]=select * from Product where parent_Id=$id AND type='product'
    
    Foreach ($Products as $Product)
    IF (count(get_variant())>=1)
         {echo "Post_title";
         content();
    //display or return post meta data from $product
    }
    
    function get_variant()
    {$id=get_parent_id();
    $variant[]=select * from Product where parent_Id=$id AND type='variant'
    //display or return post meta data from $variant
    }
    
    $var=get_post_meta (post_id(), 'variant');
    
    switch ($var)
    case 'collection':
    get_collection();
    break;
    
    case 'product':
    get_product();
    break;
    case 'variant':
    get_variant();
    break;

    Again, that was a very rough outline of something that could work. Then I could just upload collections, products, and variants all in one custom post type with hierachical pages.

    I am probably missing a variable that relates the functions together, but I think you get the idea. Do you think this would be easier than trying to deal with two different post types and a taxonomy that doesn’t want to display in the permalink?

    Moderator bcworkz

    (@bcworkz)

    I’m a bit leery that I’m still not fully grasping your product scheme, but if I understand it correctly, the hierarchical CPT tree sounds like a great approach! Then the taxonomy can be used independently as an alternative search approach without need to worry about maintaining a specific hierarchy, since that is maintained by the CPT.

    The breadcrumbs should work well this way since for any page, script can just walk up the parent tree to top level to generate the trail. I think you still may have issues if you want the permalinks to essentially reflect the breadcrumbs. It may be fine, I just don’t know. I would setup a little test to see if it’s easy to get working or not. OTOH, if permalinks can simply be post titles without the trail leading there, you of course will not have any issues with permalinks.

    I actually like blocking out code using pseudo code instead of correct syntax. It’s easier to get ideas and concepts down in writing without getting lost in distracting details of correct syntax. I’m not real strong with SQL, but I know enough to understand where you’re going, so that’s fine too.

    For production code, SQL is OK still, but consider translating to WP_Query, mainly so pagination is mostly handled for you by WP functions. There is also some minor caching that can be helpful. If you stay with SQL, you’ll need to manage pagination yourself, which isn’t that big of a deal, but one more thing to fuss with.

    In closing, I think you’re on the right path and it’s worth developing what you have further. The only doubt in my mind relates to what you want for permalinks, and that’s easy enough to set up a trial if need be.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘loop to display taxonomy terms and description’ is closed to new replies.