1

I created a two loop into my WordPress page.

It should get the category name and should load all the child under the Category name.

Like this

____ Cat1 ______

product 1 product 2 product 3

____ Cat2 ______

product 1 product 2 product 3

So the thing is, while the products are looping, the Category name is also looping, so if I have 3 products under the cat name i will have 3 cat name as well.

Here is my code:

<div class="pr_grid_cell clearfix">
<?php
//for each category, show posts
$categories=get_categories();
  foreach($categories as $cat) {
    $args=array(
      'post_type' => 'products',
      'showposts' => -1,
      'category__in' => array($cat->term_id)
    );
    $posts=get_posts($args);
      if ($posts) { ?>
        <div class="pr_grid buy-online">
            <span class="hcenter">
            <h5><?= $cat->name ?></h5>
         <? foreach($posts as $post) {
          setup_postdata($post); ?>
            <? if( get_field('buy_online_href') ): ?>
                        <?php // create our link now that the post is setup ?>
                        <span class="pr_img_href" href="<? the_permalink(); ?>">
                            <img class="ii" src="<?php the_field('product_thumbnail'); ?>">
                            <a class="button" target="_blank" href="<?php the_field('buy_online_href'); ?>">Buy Online</a>
                            <a class="button" target="_blank" href="/store-locator">Find Near you</a>
                        </span>
            <? endif ?>          
            <?php
        } // foreach($posts
      } // if ($posts
    } // foreach($categories
?>
            </span>
        </div> 
</div>

Could you guys please look into my code and correct me?

Any help would be appreciated.

5
  • 2
    There are a lot of small issues here and I'd recommend cleaning them up if you want to get a good answer. 1) Use proper full <?php openings. 2) Clean up the indenting of your code so it's readable. This in fact may show you where your look is going wrong. 3) query_posts() is evil! Don't use it! Use pre_get_posts or WP_Query instead. Google all three to learn the differences. 4) This may be your issue, but I don't see any endforeach for your foreach loop.
    – mrwweb
    Commented Jan 6, 2015 at 22:32
  • Hint: When you replace the : endforeach and : endif etc. with proper curly brackets {}, then with an IDE one will be able to quickly look at your code to see if there's something missing.
    – kaiser
    Commented Jan 7, 2015 at 0:05
  • Ok, I read through the code and there's one obsolete/nothing-matching }/closing bracket at the end. I saw a bunch of missing ;/semicolons and as @mrwweb already suggested, you are destroying & overwriting the main query with your query_posts() once per loop. Do you have debugging enabled (see Codex about that)? And do you have short PHP tags on your server enabled? Update your question (instead of commenting) with your answers. And please: Fix intending your code. That is highly unreadable that way.
    – kaiser
    Commented Jan 7, 2015 at 0:13
  • 2
    +1 to avoiding query_posts
    – Tom J Nowell
    Commented Jan 7, 2015 at 0:14
  • Alright @kaiser, i just updated my code with less code and cleaner loop. So the thing which i want is just show the category nam when the field "buy_online_href" has value. But i can't figure it out how.
    – Amir
    Commented Jan 7, 2015 at 17:21

1 Answer 1

0
<div class="pr_grid_cell clearfix">
<?php
//for each category, show posts
$categories=get_categories();
foreach($categories as $cat) {
$args=array(
  'post_type' => 'products',
  'showposts' => -1,
  'category__in' => array($cat->term_id)
);
$posts = new wp_query($args); // This is the best way to 
  if ($posts->have_posts()) while ($posts->have_posts()) { //condional if/while 2-in-1. If you dont check have_posts, you'll get an ugly error when nothings found. 
    the_post();//increments wps internal counter, Needed so that your loop doesnt run forever. 
    ?>

    <div class="pr_grid buy-online">
        <span class="hcenter">
        <h5><?= $cat->name ?></h5>
     <? foreach($posts as $post) {
      setup_postdata($post); ?>
        <? if( get_field('buy_online_href') ): ?>
                    <?php // create our link now that the post is setup ?>
                    <span class="pr_img_href" href="<? the_permalink(); ?>">
                        <img class="ii" src="<?php the_field('product_thumbnail'); ?>">
                        <a class="button" target="_blank" href="<?php the_field('buy_online_href'); ?>">Buy Online</a>
                        <a class="button" target="_blank" href="/store-locator">Find Near you</a>
                    </span>
        <? endif ?>          
        <?php
    } // foreach($posts
  } // if ($posts
} // foreach($categories
?>
        </span>
    </div> 
</div>

As mentioned, avoid using query_posts. In pretty much every case, it's cleaner to just instantiate your own instance of the wp_query. I think the one thing missing, which is why it was running infinitely, was the lack of the_post(). That incremements the "counter" that tells the loop when it's done. Without it, it will just run indefinitely.

2
  • Thanks, i actually solve the loop issue thanks to peoples comment, my code now runs very nicely, i just have another issue which is i wanted to show category name just when "buy_online_href" has value.
    – Amir
    Commented Jan 7, 2015 at 18:55
  • Code still has short PHP brackets, missing semicolons, unnecessarily doubled opening/closing tags, etc.
    – kaiser
    Commented Jan 7, 2015 at 19:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.