Viewing 3 replies - 1 through 3 (of 3 total)
  • It looks like you’re mixing blog terms with woo terms but you may get a better answer at the woo support area.
    PS: Put code in between backticks – otherwise it can get magled.

    Thread Starter michaelnewsomejr

    (@michaelnewsomejr)

    I use the following code to list all “categories” of the “product_cat” taxonomy. So, I assumed I would be able to also use to blog terms to display the categories of the product_cat taxonomy?

    <?php
    //list terms in a given taxonomy using wp_list_categories (also useful as a widget if using a PHP Code plugin)
    
    $taxonomy     = 'product_cat';
    $orderby      = 'slug';
    $show_count   = 0;      // 1 for yes, 0 for no
    $pad_counts   = 0;      // 1 for yes, 0 for no
    $hierarchical = 1;      // 1 for yes, 0 for no
    $title        = '';
    
    $args = array(
      'taxonomy'     => $taxonomy,
      'orderby'      => $orderby,
      'show_count'   => $show_count,
      'pad_counts'   => $pad_counts,
      'hierarchical' => $hierarchical,
      'title_li'     => $title
    );
    ?>
    <div style="font-size:20px;float:left;margin-top:22px;"><span style="color:#E77738;">Best Selling</span><span style="color:6c6c6c;"> Products</span></div>
    <ul id="products-grid-categories-list">
    <?php wp_list_categories( $args ); ?>
    </ul>
    Thread Starter michaelnewsomejr

    (@michaelnewsomejr)

    By the way, here is my code in question more organized:

    <?php
    $taxonomy = 'product_cat';
    $orderby = 'slug';
    $show_count = 0; // 1 for yes, 0 for no
    $pad_counts = 0; // 1 for yes, 0 for no
    $hierarchical = 1; // 1 for yes, 0 for no
    $title = '';
    $empty = 0;
    
    $args = array(
    //'taxonomy' => $taxonomy,
    'orderby' => $orderby,
    'show_count' => $show_count,
    'pad_counts' => $pad_counts,
    'hierarchical' => $hierarchical,
    'title_li' => $title,
    'hide_empty' => $empty
    );
    ?>
    <?php $all_categories = get_categories( $args );
      foreach($all_categories as $category) {
          $posts=get_posts('taxonomy=product_cat&hierarchical=1&showposts=1&cat='.$category->term_id);
          if ($posts) {
            echo '<div id="'.$category->slug.'"><p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>'.$category->name.'</a> </p> </div>';
            foreach($posts as $post) {
              setup_postdata($post); ?>
              <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
              <?php
            } // foreach($posts
          } // if ($posts
        } // foreach($categories
    ?>
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Show 1 product from each category’ is closed to new replies.