Woocommerce Product Loop With Categories Filter





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















I am trying to integrate a woocommerce product loop to use a category filter. Currently it works well if with the normal post categories but I can't get it to work with woocommerce product categories without breaking it. I have commented the parts that are giving me problem in the codes as shown below.



<ul class="portfolio-filter">
<li><a class="active" href="#" data-filter="*">All</a></li>
<?php
$args = array(
'hide_empty'=> 0,
'orderby' => 'name',
'order' => 'ASC'
);

//I want to list woocommerce categories
$categories = get_categories($args);
foreach($categories as $category) {
echo
'<li>
<a href="#" data-filter=".'.$category->name.'">
'.$category->name.'
</a>
</li>';
}
?>

</ul>

<div class="row">
<div class="portfolio-items">

<?php
//i want to replace 'cat' => $category_id with recommended woocommerce cat id
$args = array( 'post_type' => 'product', 'stock' => 1, 'posts_per_page' => 4, 'cat' => $category_id, 'orderby' =>'date','order' => 'DESC' );

$loop = new WP_Query( $args );
while ( $loop->have_posts()) : $loop->the_post(); global $product;
if ( $attachments = get_children(
array(
'post_type' => 'attachment',
'post_mime_type'=>'image',
'numberposts' => 99,// -1 to get all images
'post_status' => null,
'post_parent' => $post->ID
)
));
?>
<!--I want to display the product category name as a class to replace $category as shown below-->
<div class="col-lg-4 col-sm-6 col-md-4 item-mgn portfolio-item <?php $category = get_the_category( $post->ID );
echo $category[0]->cat_name;?>">
<div class="project_thumb">
<?php the_post_thumbnail(); ?>
</div>
<div class="project_cont">
<a href="<?php the_permalink(); ?>">
<h3 class="boxeq"><?php the_title(); ?></h3>
</a>


<a href="<?php the_permalink(); ?>"><button class="btn-green">See Features</button></a>

<a href="#"><button class="btn-line">Acquire Property</button></a>
</div>
</div>


<?php
endwhile;
wp_reset_postdata();
?>

</div><!--portfolio-items-->









share|improve this question































    1















    I am trying to integrate a woocommerce product loop to use a category filter. Currently it works well if with the normal post categories but I can't get it to work with woocommerce product categories without breaking it. I have commented the parts that are giving me problem in the codes as shown below.



    <ul class="portfolio-filter">
    <li><a class="active" href="#" data-filter="*">All</a></li>
    <?php
    $args = array(
    'hide_empty'=> 0,
    'orderby' => 'name',
    'order' => 'ASC'
    );

    //I want to list woocommerce categories
    $categories = get_categories($args);
    foreach($categories as $category) {
    echo
    '<li>
    <a href="#" data-filter=".'.$category->name.'">
    '.$category->name.'
    </a>
    </li>';
    }
    ?>

    </ul>

    <div class="row">
    <div class="portfolio-items">

    <?php
    //i want to replace 'cat' => $category_id with recommended woocommerce cat id
    $args = array( 'post_type' => 'product', 'stock' => 1, 'posts_per_page' => 4, 'cat' => $category_id, 'orderby' =>'date','order' => 'DESC' );

    $loop = new WP_Query( $args );
    while ( $loop->have_posts()) : $loop->the_post(); global $product;
    if ( $attachments = get_children(
    array(
    'post_type' => 'attachment',
    'post_mime_type'=>'image',
    'numberposts' => 99,// -1 to get all images
    'post_status' => null,
    'post_parent' => $post->ID
    )
    ));
    ?>
    <!--I want to display the product category name as a class to replace $category as shown below-->
    <div class="col-lg-4 col-sm-6 col-md-4 item-mgn portfolio-item <?php $category = get_the_category( $post->ID );
    echo $category[0]->cat_name;?>">
    <div class="project_thumb">
    <?php the_post_thumbnail(); ?>
    </div>
    <div class="project_cont">
    <a href="<?php the_permalink(); ?>">
    <h3 class="boxeq"><?php the_title(); ?></h3>
    </a>


    <a href="<?php the_permalink(); ?>"><button class="btn-green">See Features</button></a>

    <a href="#"><button class="btn-line">Acquire Property</button></a>
    </div>
    </div>


    <?php
    endwhile;
    wp_reset_postdata();
    ?>

    </div><!--portfolio-items-->









    share|improve this question



























      1












      1








      1








      I am trying to integrate a woocommerce product loop to use a category filter. Currently it works well if with the normal post categories but I can't get it to work with woocommerce product categories without breaking it. I have commented the parts that are giving me problem in the codes as shown below.



      <ul class="portfolio-filter">
      <li><a class="active" href="#" data-filter="*">All</a></li>
      <?php
      $args = array(
      'hide_empty'=> 0,
      'orderby' => 'name',
      'order' => 'ASC'
      );

      //I want to list woocommerce categories
      $categories = get_categories($args);
      foreach($categories as $category) {
      echo
      '<li>
      <a href="#" data-filter=".'.$category->name.'">
      '.$category->name.'
      </a>
      </li>';
      }
      ?>

      </ul>

      <div class="row">
      <div class="portfolio-items">

      <?php
      //i want to replace 'cat' => $category_id with recommended woocommerce cat id
      $args = array( 'post_type' => 'product', 'stock' => 1, 'posts_per_page' => 4, 'cat' => $category_id, 'orderby' =>'date','order' => 'DESC' );

      $loop = new WP_Query( $args );
      while ( $loop->have_posts()) : $loop->the_post(); global $product;
      if ( $attachments = get_children(
      array(
      'post_type' => 'attachment',
      'post_mime_type'=>'image',
      'numberposts' => 99,// -1 to get all images
      'post_status' => null,
      'post_parent' => $post->ID
      )
      ));
      ?>
      <!--I want to display the product category name as a class to replace $category as shown below-->
      <div class="col-lg-4 col-sm-6 col-md-4 item-mgn portfolio-item <?php $category = get_the_category( $post->ID );
      echo $category[0]->cat_name;?>">
      <div class="project_thumb">
      <?php the_post_thumbnail(); ?>
      </div>
      <div class="project_cont">
      <a href="<?php the_permalink(); ?>">
      <h3 class="boxeq"><?php the_title(); ?></h3>
      </a>


      <a href="<?php the_permalink(); ?>"><button class="btn-green">See Features</button></a>

      <a href="#"><button class="btn-line">Acquire Property</button></a>
      </div>
      </div>


      <?php
      endwhile;
      wp_reset_postdata();
      ?>

      </div><!--portfolio-items-->









      share|improve this question
















      I am trying to integrate a woocommerce product loop to use a category filter. Currently it works well if with the normal post categories but I can't get it to work with woocommerce product categories without breaking it. I have commented the parts that are giving me problem in the codes as shown below.



      <ul class="portfolio-filter">
      <li><a class="active" href="#" data-filter="*">All</a></li>
      <?php
      $args = array(
      'hide_empty'=> 0,
      'orderby' => 'name',
      'order' => 'ASC'
      );

      //I want to list woocommerce categories
      $categories = get_categories($args);
      foreach($categories as $category) {
      echo
      '<li>
      <a href="#" data-filter=".'.$category->name.'">
      '.$category->name.'
      </a>
      </li>';
      }
      ?>

      </ul>

      <div class="row">
      <div class="portfolio-items">

      <?php
      //i want to replace 'cat' => $category_id with recommended woocommerce cat id
      $args = array( 'post_type' => 'product', 'stock' => 1, 'posts_per_page' => 4, 'cat' => $category_id, 'orderby' =>'date','order' => 'DESC' );

      $loop = new WP_Query( $args );
      while ( $loop->have_posts()) : $loop->the_post(); global $product;
      if ( $attachments = get_children(
      array(
      'post_type' => 'attachment',
      'post_mime_type'=>'image',
      'numberposts' => 99,// -1 to get all images
      'post_status' => null,
      'post_parent' => $post->ID
      )
      ));
      ?>
      <!--I want to display the product category name as a class to replace $category as shown below-->
      <div class="col-lg-4 col-sm-6 col-md-4 item-mgn portfolio-item <?php $category = get_the_category( $post->ID );
      echo $category[0]->cat_name;?>">
      <div class="project_thumb">
      <?php the_post_thumbnail(); ?>
      </div>
      <div class="project_cont">
      <a href="<?php the_permalink(); ?>">
      <h3 class="boxeq"><?php the_title(); ?></h3>
      </a>


      <a href="<?php the_permalink(); ?>"><button class="btn-green">See Features</button></a>

      <a href="#"><button class="btn-line">Acquire Property</button></a>
      </div>
      </div>


      <?php
      endwhile;
      wp_reset_postdata();
      ?>

      </div><!--portfolio-items-->






      php wordpress loops woocommerce






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 23 '18 at 0:02







      Sam

















      asked Nov 22 '18 at 10:16









      SamSam

      9010




      9010
























          1 Answer
          1






          active

          oldest

          votes


















          1














          I was finally able to solve this problem. For the sake of those that face the same problem that might come across this, below is the updated working code for your perusal.



          <ul class="portfolio-filter">
          <li>
          <a class="active" href="#" data-filter="*">All</a>
          </li>
          <?php
          $args = array(
          'hide_empty'=> 0,
          'orderby' => 'name',
          'order' => 'ASC'
          );

          $product_categories = get_terms( 'product_cat', $cat_args );
          foreach ($product_categories as $key => $category) {
          echo
          '<li>
          <a href="#" data-filter=".'.$category->name.'">
          '.$category->name.'
          </a>
          </li>';
          }
          ?>

          </ul>

          <div class="row">
          <div class="portfolio-items">

          <?php

          $args = array( 'post_type' => 'product', 'stock' => 1, 'posts_per_page' => -1, 'cat' => $category_id, 'orderby' =>'date','order' => 'DESC' );

          $loop = new WP_Query( $args );
          while ( $loop->have_posts()) : $loop->the_post(); global $product;
          if ( $attachments = get_children(
          array(
          'post_type' => 'attachment',
          'post_mime_type'=>'image',
          'numberposts' => 99,// -1 to get all images
          'post_status' => null,
          'post_parent' => $post->ID
          )
          ));

          global $wp_query;
          $terms_post = get_the_terms( $post->cat_ID , 'product_cat' );
          foreach ($terms_post as $term_cat) {

          }
          ?>

          <div class="col-lg-4 col-sm-6 col-md-4 item-mgn portfolio-item <?php echo $term_cat->name; ?>">
          <div class="project_thumb">
          <?php the_post_thumbnail(); ?>
          </div>
          <div class="project_cont">
          <a href="<?php the_permalink(); ?>">
          <h3 class="boxeq"><?php the_title(); ?></h3>
          </a>


          <a href="<?php the_permalink(); ?>"><button class="btn-green">See Features</button></a>

          <a href="#"><button class="btn-line">Acquire Property</button></a>
          </div>
          </div>


          <?php
          endwhile;
          wp_reset_postdata();
          ?>

          </div><!--portfolio-items-->

          </div>





          share|improve this answer
























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53428635%2fwoocommerce-product-loop-with-categories-filter%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            I was finally able to solve this problem. For the sake of those that face the same problem that might come across this, below is the updated working code for your perusal.



            <ul class="portfolio-filter">
            <li>
            <a class="active" href="#" data-filter="*">All</a>
            </li>
            <?php
            $args = array(
            'hide_empty'=> 0,
            'orderby' => 'name',
            'order' => 'ASC'
            );

            $product_categories = get_terms( 'product_cat', $cat_args );
            foreach ($product_categories as $key => $category) {
            echo
            '<li>
            <a href="#" data-filter=".'.$category->name.'">
            '.$category->name.'
            </a>
            </li>';
            }
            ?>

            </ul>

            <div class="row">
            <div class="portfolio-items">

            <?php

            $args = array( 'post_type' => 'product', 'stock' => 1, 'posts_per_page' => -1, 'cat' => $category_id, 'orderby' =>'date','order' => 'DESC' );

            $loop = new WP_Query( $args );
            while ( $loop->have_posts()) : $loop->the_post(); global $product;
            if ( $attachments = get_children(
            array(
            'post_type' => 'attachment',
            'post_mime_type'=>'image',
            'numberposts' => 99,// -1 to get all images
            'post_status' => null,
            'post_parent' => $post->ID
            )
            ));

            global $wp_query;
            $terms_post = get_the_terms( $post->cat_ID , 'product_cat' );
            foreach ($terms_post as $term_cat) {

            }
            ?>

            <div class="col-lg-4 col-sm-6 col-md-4 item-mgn portfolio-item <?php echo $term_cat->name; ?>">
            <div class="project_thumb">
            <?php the_post_thumbnail(); ?>
            </div>
            <div class="project_cont">
            <a href="<?php the_permalink(); ?>">
            <h3 class="boxeq"><?php the_title(); ?></h3>
            </a>


            <a href="<?php the_permalink(); ?>"><button class="btn-green">See Features</button></a>

            <a href="#"><button class="btn-line">Acquire Property</button></a>
            </div>
            </div>


            <?php
            endwhile;
            wp_reset_postdata();
            ?>

            </div><!--portfolio-items-->

            </div>





            share|improve this answer




























              1














              I was finally able to solve this problem. For the sake of those that face the same problem that might come across this, below is the updated working code for your perusal.



              <ul class="portfolio-filter">
              <li>
              <a class="active" href="#" data-filter="*">All</a>
              </li>
              <?php
              $args = array(
              'hide_empty'=> 0,
              'orderby' => 'name',
              'order' => 'ASC'
              );

              $product_categories = get_terms( 'product_cat', $cat_args );
              foreach ($product_categories as $key => $category) {
              echo
              '<li>
              <a href="#" data-filter=".'.$category->name.'">
              '.$category->name.'
              </a>
              </li>';
              }
              ?>

              </ul>

              <div class="row">
              <div class="portfolio-items">

              <?php

              $args = array( 'post_type' => 'product', 'stock' => 1, 'posts_per_page' => -1, 'cat' => $category_id, 'orderby' =>'date','order' => 'DESC' );

              $loop = new WP_Query( $args );
              while ( $loop->have_posts()) : $loop->the_post(); global $product;
              if ( $attachments = get_children(
              array(
              'post_type' => 'attachment',
              'post_mime_type'=>'image',
              'numberposts' => 99,// -1 to get all images
              'post_status' => null,
              'post_parent' => $post->ID
              )
              ));

              global $wp_query;
              $terms_post = get_the_terms( $post->cat_ID , 'product_cat' );
              foreach ($terms_post as $term_cat) {

              }
              ?>

              <div class="col-lg-4 col-sm-6 col-md-4 item-mgn portfolio-item <?php echo $term_cat->name; ?>">
              <div class="project_thumb">
              <?php the_post_thumbnail(); ?>
              </div>
              <div class="project_cont">
              <a href="<?php the_permalink(); ?>">
              <h3 class="boxeq"><?php the_title(); ?></h3>
              </a>


              <a href="<?php the_permalink(); ?>"><button class="btn-green">See Features</button></a>

              <a href="#"><button class="btn-line">Acquire Property</button></a>
              </div>
              </div>


              <?php
              endwhile;
              wp_reset_postdata();
              ?>

              </div><!--portfolio-items-->

              </div>





              share|improve this answer


























                1












                1








                1







                I was finally able to solve this problem. For the sake of those that face the same problem that might come across this, below is the updated working code for your perusal.



                <ul class="portfolio-filter">
                <li>
                <a class="active" href="#" data-filter="*">All</a>
                </li>
                <?php
                $args = array(
                'hide_empty'=> 0,
                'orderby' => 'name',
                'order' => 'ASC'
                );

                $product_categories = get_terms( 'product_cat', $cat_args );
                foreach ($product_categories as $key => $category) {
                echo
                '<li>
                <a href="#" data-filter=".'.$category->name.'">
                '.$category->name.'
                </a>
                </li>';
                }
                ?>

                </ul>

                <div class="row">
                <div class="portfolio-items">

                <?php

                $args = array( 'post_type' => 'product', 'stock' => 1, 'posts_per_page' => -1, 'cat' => $category_id, 'orderby' =>'date','order' => 'DESC' );

                $loop = new WP_Query( $args );
                while ( $loop->have_posts()) : $loop->the_post(); global $product;
                if ( $attachments = get_children(
                array(
                'post_type' => 'attachment',
                'post_mime_type'=>'image',
                'numberposts' => 99,// -1 to get all images
                'post_status' => null,
                'post_parent' => $post->ID
                )
                ));

                global $wp_query;
                $terms_post = get_the_terms( $post->cat_ID , 'product_cat' );
                foreach ($terms_post as $term_cat) {

                }
                ?>

                <div class="col-lg-4 col-sm-6 col-md-4 item-mgn portfolio-item <?php echo $term_cat->name; ?>">
                <div class="project_thumb">
                <?php the_post_thumbnail(); ?>
                </div>
                <div class="project_cont">
                <a href="<?php the_permalink(); ?>">
                <h3 class="boxeq"><?php the_title(); ?></h3>
                </a>


                <a href="<?php the_permalink(); ?>"><button class="btn-green">See Features</button></a>

                <a href="#"><button class="btn-line">Acquire Property</button></a>
                </div>
                </div>


                <?php
                endwhile;
                wp_reset_postdata();
                ?>

                </div><!--portfolio-items-->

                </div>





                share|improve this answer













                I was finally able to solve this problem. For the sake of those that face the same problem that might come across this, below is the updated working code for your perusal.



                <ul class="portfolio-filter">
                <li>
                <a class="active" href="#" data-filter="*">All</a>
                </li>
                <?php
                $args = array(
                'hide_empty'=> 0,
                'orderby' => 'name',
                'order' => 'ASC'
                );

                $product_categories = get_terms( 'product_cat', $cat_args );
                foreach ($product_categories as $key => $category) {
                echo
                '<li>
                <a href="#" data-filter=".'.$category->name.'">
                '.$category->name.'
                </a>
                </li>';
                }
                ?>

                </ul>

                <div class="row">
                <div class="portfolio-items">

                <?php

                $args = array( 'post_type' => 'product', 'stock' => 1, 'posts_per_page' => -1, 'cat' => $category_id, 'orderby' =>'date','order' => 'DESC' );

                $loop = new WP_Query( $args );
                while ( $loop->have_posts()) : $loop->the_post(); global $product;
                if ( $attachments = get_children(
                array(
                'post_type' => 'attachment',
                'post_mime_type'=>'image',
                'numberposts' => 99,// -1 to get all images
                'post_status' => null,
                'post_parent' => $post->ID
                )
                ));

                global $wp_query;
                $terms_post = get_the_terms( $post->cat_ID , 'product_cat' );
                foreach ($terms_post as $term_cat) {

                }
                ?>

                <div class="col-lg-4 col-sm-6 col-md-4 item-mgn portfolio-item <?php echo $term_cat->name; ?>">
                <div class="project_thumb">
                <?php the_post_thumbnail(); ?>
                </div>
                <div class="project_cont">
                <a href="<?php the_permalink(); ?>">
                <h3 class="boxeq"><?php the_title(); ?></h3>
                </a>


                <a href="<?php the_permalink(); ?>"><button class="btn-green">See Features</button></a>

                <a href="#"><button class="btn-line">Acquire Property</button></a>
                </div>
                </div>


                <?php
                endwhile;
                wp_reset_postdata();
                ?>

                </div><!--portfolio-items-->

                </div>






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 23 '18 at 0:01









                SamSam

                9010




                9010
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53428635%2fwoocommerce-product-loop-with-categories-filter%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Guess what letter conforming each word

                    Port of Spain

                    Run scheduled task as local user group (not BUILTIN)