我正在尝试循环浏览自定义WordPress帖子,遇到了一个问题,比如我添加了自定义字段并想在中显示它<li>
使用循环。我成功地完成了操作,但数据/链接/类别正在重复,如果类别与以下内容相同,我希望只显示一次:
如果我有2篇带有data1类别的帖子,那么链接将只显示data1once 但我有2个不同类别的帖子,然后它会分别显示每个帖子。
Sample Code:
<ul class="filter filter-top">
<li class="active"><a href="#" data-filter="*">All</a></li>
<?php
$portfolio_args_custom = array(
\'post_type\' => \'portfolio\',
);
$portfolio_posts_custom = new WP_Query($portfolio_args_custom);
while($portfolio_posts_custom->have_posts()) {
$portfolio_posts_custom->the_post();
?>
<li><a href="#" data-filter=".<?php echo get_post_meta( get_the_ID(), \'Category\', true);?>"><?php echo get_post_meta( get_the_ID(), \'Category\', true);?></a></li>
<?php wp_reset_postdata();
wp_reset_query();
}?>
</ul>
代码运行正常,但它正在重复<li>
具有相同类别的项目。请提供任何文档/帮助链接/建议。Edit:
这个代码按照我的要求工作,但我不清楚,如果可能的话请解释。 <ul class="filter filter-top">
<li class="active"><a href="#" data-filter="*">All</a></li>
<?php
$portfolio_args_custom = array(
\'post_type\' => \'portfolio\',
);
$portfolio_posts_custom = new WP_Query($portfolio_args_custom);
while($portfolio_posts_custom->have_posts()) {
$portfolio_posts_custom->the_post();
$categories = get_the_category();
foreach ( $categories as $key=>$category ) {
?>
<li><a href="#" data-filter=".<?php echo get_post_meta( get_the_ID(), \'Category\', true);?>"><?php echo get_post_meta( get_the_ID(), \'Category\', true);?></a></li>
<?php wp_reset_postdata();
wp_reset_query();
}
}
?>
</ul>