admin管理员组文章数量:1130349
I have this function that displays the specified hierarchy level category of a post. For example <?php display_cat_level(0,true); ?> will display the top/parent category, while <?php display_cat_level(2,true); ?> will display the second child category.
If the post has more than one category in any given level then the categories will be displayed inline with no spaces or commas between them like this cat1acat1bcat1c instead of cat1a, cat1b, cat1c. How would I update the function to comma separate multiple categories displayed?
function get_level($category, $level = 0)
{
if ($category->parent == 0) {
return $level;
} else {
$level++;
$category = get_term( $category->parent );
return get_level($category, $level);
}
}
function display_cat_level( $level = 0 , $link=false){
$cats = get_the_terms( null, 'category' );
if( $cats ){
foreach($cats as $cat){
$current_cat_level = get_level($cat);
if( $current_cat_level == $level ){
if($link==true) {
echo '<a href="'.get_term_link( $cat->term_id ).'">'.$cat->name."</a>";
} else {
echo $cat->name."";
}
}
}
}
}
I have this function that displays the specified hierarchy level category of a post. For example <?php display_cat_level(0,true); ?> will display the top/parent category, while <?php display_cat_level(2,true); ?> will display the second child category.
If the post has more than one category in any given level then the categories will be displayed inline with no spaces or commas between them like this cat1acat1bcat1c instead of cat1a, cat1b, cat1c. How would I update the function to comma separate multiple categories displayed?
function get_level($category, $level = 0)
{
if ($category->parent == 0) {
return $level;
} else {
$level++;
$category = get_term( $category->parent );
return get_level($category, $level);
}
}
function display_cat_level( $level = 0 , $link=false){
$cats = get_the_terms( null, 'category' );
if( $cats ){
foreach($cats as $cat){
$current_cat_level = get_level($cat);
if( $current_cat_level == $level ){
if($link==true) {
echo '<a href="'.get_term_link( $cat->term_id ).'">'.$cat->name."</a>";
} else {
echo $cat->name."";
}
}
}
}
}
Share
Improve this question
edited Nov 25, 2018 at 8:30
Krzysiek Dróżdż
25.6k9 gold badges53 silver badges74 bronze badges
asked Nov 25, 2018 at 7:46
PetePete
1,0582 gold badges14 silver badges40 bronze badges
1 Answer
Reset to default 1All you have to do is to track if you already echoed some category and add a comma in such case:
function get_level($category, $level = 0) {
if ($category->parent == 0) {
return $level;
}
$category = get_term( $category->parent );
return get_level( $category, $level + 1 );
}
function display_cat_level( $level = 0, $link = false) {
$cats = get_the_terms( null, 'category' );
$echoed = 0;
if ( $cats ) {
foreach ( $cats as $cat ) {
$current_cat_level = get_level( $cat );
if ( $current_cat_level == $level ) {
if ( $echoed ) {
echo ', ';
}
if ( true == $link ) {
echo '<a href="' . get_term_link( $cat->term_id ) . '">' . esc_html( $cat->name ) . "</a>";
} else {
echo esc_html( $cat->name );
}
$echoed++;
}
}
}
}
PS. I've also simplified your get_level function a little bit and added some html escaping in display_cat_level.
I have this function that displays the specified hierarchy level category of a post. For example <?php display_cat_level(0,true); ?> will display the top/parent category, while <?php display_cat_level(2,true); ?> will display the second child category.
If the post has more than one category in any given level then the categories will be displayed inline with no spaces or commas between them like this cat1acat1bcat1c instead of cat1a, cat1b, cat1c. How would I update the function to comma separate multiple categories displayed?
function get_level($category, $level = 0)
{
if ($category->parent == 0) {
return $level;
} else {
$level++;
$category = get_term( $category->parent );
return get_level($category, $level);
}
}
function display_cat_level( $level = 0 , $link=false){
$cats = get_the_terms( null, 'category' );
if( $cats ){
foreach($cats as $cat){
$current_cat_level = get_level($cat);
if( $current_cat_level == $level ){
if($link==true) {
echo '<a href="'.get_term_link( $cat->term_id ).'">'.$cat->name."</a>";
} else {
echo $cat->name."";
}
}
}
}
}
I have this function that displays the specified hierarchy level category of a post. For example <?php display_cat_level(0,true); ?> will display the top/parent category, while <?php display_cat_level(2,true); ?> will display the second child category.
If the post has more than one category in any given level then the categories will be displayed inline with no spaces or commas between them like this cat1acat1bcat1c instead of cat1a, cat1b, cat1c. How would I update the function to comma separate multiple categories displayed?
function get_level($category, $level = 0)
{
if ($category->parent == 0) {
return $level;
} else {
$level++;
$category = get_term( $category->parent );
return get_level($category, $level);
}
}
function display_cat_level( $level = 0 , $link=false){
$cats = get_the_terms( null, 'category' );
if( $cats ){
foreach($cats as $cat){
$current_cat_level = get_level($cat);
if( $current_cat_level == $level ){
if($link==true) {
echo '<a href="'.get_term_link( $cat->term_id ).'">'.$cat->name."</a>";
} else {
echo $cat->name."";
}
}
}
}
}
Share
Improve this question
edited Nov 25, 2018 at 8:30
Krzysiek Dróżdż
25.6k9 gold badges53 silver badges74 bronze badges
asked Nov 25, 2018 at 7:46
PetePete
1,0582 gold badges14 silver badges40 bronze badges
1 Answer
Reset to default 1All you have to do is to track if you already echoed some category and add a comma in such case:
function get_level($category, $level = 0) {
if ($category->parent == 0) {
return $level;
}
$category = get_term( $category->parent );
return get_level( $category, $level + 1 );
}
function display_cat_level( $level = 0, $link = false) {
$cats = get_the_terms( null, 'category' );
$echoed = 0;
if ( $cats ) {
foreach ( $cats as $cat ) {
$current_cat_level = get_level( $cat );
if ( $current_cat_level == $level ) {
if ( $echoed ) {
echo ', ';
}
if ( true == $link ) {
echo '<a href="' . get_term_link( $cat->term_id ) . '">' . esc_html( $cat->name ) . "</a>";
} else {
echo esc_html( $cat->name );
}
$echoed++;
}
}
}
}
PS. I've also simplified your get_level function a little bit and added some html escaping in display_cat_level.
本文标签: phpHow to separate categories with commas
版权声明:本文标题:php - How to separate categories with commas? 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749152594a2324074.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论