我用这段php代码列出了所有带有分类类别名称的帖子(标题),它工作得很好,但我想将其分解,避免在php代码中使用echo,以便插入更多代码。这是我的代码--
<?php
$custom_terms = get_terms(\'videoscategory\');
$other_custom_terms = get_terms(\'product_category\');
foreach ($custom_terms as $custom_term) {
foreach ($other_custom_terms as $other_custom_term) {
wp_reset_query();
$args = array(\'post_type\' => \'product\', \'orderby\' => \'title\',
\'order\' => \'asc\',
\'tax_query\' => array(
\'relation\' => \'AND\',
\'orderby\' => \'title\',
\'order\' => \'asc\',
array(
\'taxonomy\' => \'product_category\',
\'field\' => \'slug\',
\'terms\' => $other_custom_term->slug,
\'orderby\' => \'title\',
\'order\' => \'asc\',
),
array(
\'taxonomy\' => \'videoscategory\',
\'field\' => \'slug\',
\'terms\' => $custom_term->slug,
\'orderby\' => \'title\',
\'order\' => \'asc\',
),
),
);
$loop = new WP_Query($args);
if($loop->have_posts()) {
echo \'<h1 style="margin-top:10px;">\'.$custom_term->name.\'</h1>\';
while($loop->have_posts()) : $loop->the_post();
echo \'<h2><a href="\'.get_permalink().\'">\'.get_the_title().\'</a></h2>\';
endwhile;
}
}
} ?>
这是我现在所拥有的代码,我已经编辑了这些代码,以尝试并完成我想做的事情--
<?php
$custom_terms = get_terms(\'videoscategory\');
$other_custom_terms = get_terms(\'product_category\');
foreach ($custom_terms as $custom_term) {
foreach ($other_custom_terms as $other_custom_term) {
wp_reset_query();
$args = array(\'post_type\' => \'product\', \'orderby\' => \'title\',
\'order\' => \'asc\',
\'tax_query\' => array(
\'relation\' => \'AND\',
\'orderby\' => \'title\',
\'order\' => \'asc\',
array(
\'taxonomy\' => \'product_category\',
\'field\' => \'slug\',
\'terms\' => $other_custom_term->slug,
\'orderby\' => \'title\',
\'order\' => \'asc\',
),
array(
\'taxonomy\' => \'videoscategory\',
\'field\' => \'slug\',
\'terms\' => $custom_term->slug,
\'orderby\' => \'title\',
\'order\' => \'asc\',
),
),
);
}
}
}
$loop = new WP_Query($args);
if($loop->have_posts()) : while($loop->have_posts()) : $loop->the_post(); ?>
<div class="box <?php echo $custom_term->slug; ?>">
<h1 style="font-weight:bold;font-size:18px;margin-top:10px;"><?php echo $custom_term->name ?></h1>
<h2><a href="<?php get_permalink(); ?>"><?php get_the_title(); ?></a></h2>
</div>
<?php endwhile; ?>
基本上,我想做的是将术语名称或slug作为一个类分配给我的div,这样我就可以使用同位素过滤器按类别过滤帖子列表。然而,我收到了解析/语法错误,而且一切都不正常。我做错了什么?非常感谢您的帮助,谢谢。
最合适的回答,由SO网友:Rajeev Vyas 整理而成
您有一个额外的“}”,每个循环必须有两个这样的大括号。您还需要有endif;endwhile之后的end语句。分析错误可能是由于额外的“}”。请尝试下面的代码,并让我们知道它是否工作。
<?php
$custom_terms = get_terms(\'videoscategory\');
$other_custom_terms = get_terms(\'product_category\');
foreach ($custom_terms as $custom_term) {
foreach ($other_custom_terms as $other_custom_term) {
wp_reset_query();
$args = array(\'post_type\' => \'product\', \'orderby\' => \'title\',
\'order\' => \'asc\',
\'tax_query\' => array(
\'relation\' => \'AND\',
\'orderby\' => \'title\',
\'order\' => \'asc\',
array(
\'taxonomy\' => \'product_category\',
\'field\' => \'slug\',
\'terms\' => $other_custom_term->slug,
\'orderby\' => \'title\',
\'order\' => \'asc\',
),
array(
\'taxonomy\' => \'videoscategory\',
\'field\' => \'slug\',
\'terms\' => $custom_term->slug,
\'orderby\' => \'title\',
\'order\' => \'asc\',
),
),
);
$loop = new WP_Query($args);
if($loop->have_posts())
{
?>
<div class="box <?php echo $custom_term->slug; ?>">
<h1 style="margin-top:10px;"><?php echo $custom_term->name;?></h1>
<?php
while($loop->have_posts()) : $loop->the_post(); ?>
<h2><a href="<?php echo get_permalink() ?>"><?php echo get_the_title(); ?></a></h2>
<?php endwhile; ?>
</div>
<?php
}
}
} ?>