I have a php/wp script written which in theory should print a div with a category name, description, and all of its products inside.
The code looks like this:
<?php
$args2 = array( 'taxonomy' => 'product_cat', 'parent' => 9 );
$sub_cats = get_categories( $args2 );
foreach( $sub_cats as $sub_category ) { ?>
<div class="treatments-description col-md-9" id="<?php echo $sub_category->term_id;?>">
<h2 class="section-heading">
<span class="line-behind-text"><?php echo $sub_category->name;?></span>
</h2>
<p class="section-text">
OPIS: <?php echo category_description(); ?> //this part does not work too, not sure why
</p>
<h3 class="section-heading"><p class="line-behind-text">DostÄpne zabiegi</p></h3>
<table class="treatments-table table products">
<tr class="table-heading">
<th class="name" id="<?php echo $sub_category->term_id;?>">UsÅuga</th>
<th>Czas trwania</th>
<th>Cena</th>
<th></th>
</tr> <?php
$name = $sub_category->name;
$args = array( 'post_type' => 'product',
"product_cat" => $sub_category->term_id //PROBLEM HERE
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
$product = new WC_Product(get_the_ID()); ?>
<tr>
<td class="name"><?php the_title(); ?><p class="small"><?php the_content(); ?></p></td>
<td><?php the_excerpt(); ?></td>
<td><?php echo $product->price; ?>zÅ</td>
<td><button class="button-product materialbutton">Rezerwuj</button> </td>
</tr> <?php
endwhile;
}
else {
echo __( 'No products found' );
} ?>
<h1>THE END</h1> <?php
} //ALL UNCLOSED TAGS ARE GETTING CLOSED AFTERWARDS
Now in theory it should display a the divs like this:
- Category a:
1a. Category description
1b. Category table - Category b:
2a. Category description
2b. Category table
But instead, the outcome looks like this:
So as you see not only does it not lay out the page correctly (the order is descrption1, description2, table1, table2, mind the location of <h1>THE END</h1>
), it also doesn’t seem to correctly match the products of category. The same result happens when Id put in the array
“product_cat” => 14 //proven category id, containing posts
I’m experienced in wp, but fairly new to woocommerce. If anyone could help me with these issues, It would be gladly appreciated.
Hard to tell from your images but i’m nearly 100% you are not closing the table tags correctly and what you are describing is exactly what would happen in this case.
Problem is you open a table tag on one iteration point (foreach) and then add 2 rows, ok so far, on the next iteration point, you add a h2 tag inside the open table tag and so on.
The browser will attempt to fix these for you by closing the tags but it will not re-arrange your html for you, so the h tags are visible above the table in the rendered view.
See below: The table is only opened if there are posts. If you want one opened when no posts, you will need to close the table before declaring “no posts found” etc
For your first problem, I am pretty sure that you need to replace
category_description();
by$sub_category->description;
(in the code below is$sub_cat->description;
)For you second problem, I can’t test it and i am not completely sure, but you need something more (see in here)â¦
You need also closing your table with
</table>
(near the end) and also</div>
(at the end)