我有一个php / wp脚本,理论上应该打印一个带有类别名称,描述及其所有产品的div。 代码如下所示:
<?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
现在理论上它应该显示这样的div:
答案 0 :(得分:1)
很难从你的图片中看出,但我差不多100%你没有正确关闭表格标签,你所描述的正是在这种情况下会发生的事情。
问题是你在一个迭代点(foreach)上打开一个表标签,然后在下一个迭代点添加2行,ok,到目前为止,在open table标签内添加一个h2标签,依此类推。
浏览器会尝试通过关闭标记为您修复这些标记但不会为您重新排列html,因此h标记在渲染视图中的表格上方可见。
见下文:只有有帖子才会打开表格。如果你想在没有帖子的情况下打开一个,你需要在宣布“找不到帖子”等之前关闭表格
<?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">Dostepne zabiegi</p></h3>
<?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() ) {
//only include the table if we have posts???
echo '<table class="treatments-table table products">';
echo '<tr class="table-heading">
<th class="name" id="<?php echo $sub_category->term_id;?>">Usluga</th>
<th>Czas trwania</th>
<th>Cena</th>
<th></th>
</tr>';
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; ?>zl</td>
<td><button class="button-product materialbutton">Rezerwuj</button> </td>
</tr> <?php
endwhile;
echo '</table>';
}
else {
echo __( 'No products found' );
} ?>
<h1>THE END</h1> <?php
} //ALL UNCLOSED TAGS ARE GETTING CLOSED AFTERWARDS -- must close tags in the loop, otherwise multiple open tags!!
答案 1 :(得分:1)
对于您的第一个问题,我非常确定您需要category_description();
替换$sub_category->description;
(在下面的代码中为$sub_cat->description;
)
对于你的第二个问题,我无法测试,我不完全确定,但你需要更多的东西( see in here )......
您还需要使用</table>
(接近结尾)和</div>
(最后)关闭您的表格
<?php
$args2 = array( 'taxonomy' => 'product_cat', 'parent' => 9 );
$sub_cats = get_categories( $args2 );
foreach( $sub_cats as $sub_cat ) { ?>
$sub_cat_name = $sub_cat->name;
$sub_cat_description = $sub_cat->description; // <= Here (1)
$sub_cat_id = $sub_cat->term_id;
<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_cat_name; ?></span>
</h2>
<p class="section-text">
OPIS: <?php echo $sub_cat_description; ?>
</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_cat_id; ?>">Usługa</th>
<th>Czas trwania</th>
<th>Cena</th>
<th></th>
</tr>
<?php
global $post; // Here (2)
$terms = get_the_terms( $post->ID, 'product_cat' );
foreach ($terms as $term) {
$product_cat_id = $term->term_id;
// $product_cat_name = $term->name;
break;
}
$args = array(
'post_type' => 'product',
'product_cat' => $product_cat_id'
);
$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; ?>
</table>
<?php
}
else {
echo __( 'No products found' );
} ?>
<h1>THE END</h1>
<?php
}
?>
</div>