如何仅在类别不为空时显示类别
how to show a category only if it is not empty
我正在使用以下遗留代码提取 post 类型并在 wordpress 站点模板文件上显示记录。
<?php
$genre_ids = get_posts('fields=ids&posts_per_page=-1&post_status=publish&post_type=genre&order=asc&orderby=title');
$choices = array(array('text' => 'Select Genre From List', 'value' => 0 ));
foreach ( $genre_ids as $genre_id ) {
$categories[] = get_the_title( $genre_id );
}
if (!empty($categories)){
foreach ($categories as $i=>$cat) {
print "<li>";
print "<a href='" . get_site_url() . "/band-search-page/?cat=$cat'>" . $cat . "</a>";
print "</li>";
}
} else {
print "<li>";
print "no categories";
print "</li>";
}
脚本现在可以说如果有任何 post 类型 GENRE 然后 运行 查询。但我想编辑它以检查并仅在有 GENRE 时显示 GENRE。现在即使没有数据,查询也会显示所有类别。任何帮助将不胜感激。
请试试这个代码::
<?php
$genre_ids = get_posts('fields=ids&posts_per_page=-1&post_status=publish&post_type=genre&order=asc&orderby=title');
$choices = array(array('text' => 'Select Genre From List', 'value' => 0));
foreach ($genre_ids as $genre_id)
{
$categories[] = get_the_title($genre_id);
}
if (!empty($categories))
{
foreach ($categories as $i => $cat)
{
if (!empty($cat))
{
print "<li>";
print "<a href='" . get_site_url() . "/band-search-page/?cat=$cat'>" . $cat . "</a>";
print "</li>";
}
}
}
else
{
print "<li>";
print "no categories";
print "</li>";
}
?>
尝试替换这一行:
print "<a href='" . get_site_url() . "/band-search-page/?cat=$cat'>" . $cat . "</a>";
用这条线
print "<a href='" . get_site_url() . "/band-search-page/?cat=$cat'>" . array_values(array_filter($cat) . "</a>";
只需添加 array_filter($cat) 即可删除空数组元素。
不知道我是否理解正确,但我也在一个网站上工作,我只想显示其中存储了帖子的类别名称。我刚刚使用 if(category_count > 0)
.
检查了它
好吧,您可能想要设置类别并使用 get_categories()
调用它,遍历它然后您可以执行 category->count
检查计数。
希望这可能对...某人有帮助...:)
我正在使用以下遗留代码提取 post 类型并在 wordpress 站点模板文件上显示记录。
<?php
$genre_ids = get_posts('fields=ids&posts_per_page=-1&post_status=publish&post_type=genre&order=asc&orderby=title');
$choices = array(array('text' => 'Select Genre From List', 'value' => 0 ));
foreach ( $genre_ids as $genre_id ) {
$categories[] = get_the_title( $genre_id );
}
if (!empty($categories)){
foreach ($categories as $i=>$cat) {
print "<li>";
print "<a href='" . get_site_url() . "/band-search-page/?cat=$cat'>" . $cat . "</a>";
print "</li>";
}
} else {
print "<li>";
print "no categories";
print "</li>";
}
脚本现在可以说如果有任何 post 类型 GENRE 然后 运行 查询。但我想编辑它以检查并仅在有 GENRE 时显示 GENRE。现在即使没有数据,查询也会显示所有类别。任何帮助将不胜感激。
请试试这个代码::
<?php
$genre_ids = get_posts('fields=ids&posts_per_page=-1&post_status=publish&post_type=genre&order=asc&orderby=title');
$choices = array(array('text' => 'Select Genre From List', 'value' => 0));
foreach ($genre_ids as $genre_id)
{
$categories[] = get_the_title($genre_id);
}
if (!empty($categories))
{
foreach ($categories as $i => $cat)
{
if (!empty($cat))
{
print "<li>";
print "<a href='" . get_site_url() . "/band-search-page/?cat=$cat'>" . $cat . "</a>";
print "</li>";
}
}
}
else
{
print "<li>";
print "no categories";
print "</li>";
}
?>
尝试替换这一行:
print "<a href='" . get_site_url() . "/band-search-page/?cat=$cat'>" . $cat . "</a>";
用这条线
print "<a href='" . get_site_url() . "/band-search-page/?cat=$cat'>" . array_values(array_filter($cat) . "</a>";
只需添加 array_filter($cat) 即可删除空数组元素。
不知道我是否理解正确,但我也在一个网站上工作,我只想显示其中存储了帖子的类别名称。我刚刚使用 if(category_count > 0)
.
好吧,您可能想要设置类别并使用 get_categories()
调用它,遍历它然后您可以执行 category->count
检查计数。
希望这可能对...某人有帮助...:)