每一期的PHP

每一期的PHP,php,Php,我正在使用foreach函数获取WordPress站点的所有类别,如果有其他标记,我需要一种方法来调整输出,以包括动态CSS 基本上,如果我只有一个标记,我需要代码输出如下所示: .category-wordpress #wordpress { background: #FFFFFF; color: #232323; text-decoration: none; } .category-wordpress #wordpress, .category-html #html { backgroun

我正在使用foreach函数获取WordPress站点的所有类别,如果有其他标记,我需要一种方法来调整输出,以包括动态CSS

基本上,如果我只有一个标记,我需要代码输出如下所示:

.category-wordpress #wordpress { background: #FFFFFF; color: #232323; text-decoration: none; }
.category-wordpress #wordpress, .category-html #html { background: #FFFFFF; color: #232323; text-decoration: none; }
.category-wordpress #wordpress, .category-html #html, { background: #FFFFFF; color: #232323; text-decoration: none; }
.category-wordpress #wordpress { background: #FFFFFF; color: #232323; text-decoration: none; }
.category-html #html { background: #FFFFFF; color: #232323; text-decoration: none; }
如果有多个这样的标签:

.category-wordpress #wordpress { background: #FFFFFF; color: #232323; text-decoration: none; }
.category-wordpress #wordpress, .category-html #html { background: #FFFFFF; color: #232323; text-decoration: none; }
.category-wordpress #wordpress, .category-html #html, { background: #FFFFFF; color: #232323; text-decoration: none; }
.category-wordpress #wordpress { background: #FFFFFF; color: #232323; text-decoration: none; }
.category-html #html { background: #FFFFFF; color: #232323; text-decoration: none; }
问题在于声明之间的逗号。如何更改此代码,以便只有在包含其他类别时才包含逗号

if (is_category()) { 

$css_cats = "<style type='text/css'>";

 //get all blog categories
 $categories = get_categories('title_li=&orderby=name&hide_empty=0');

 if ($categories) {

  foreach($categories as $category) {

   $css_cats .= ".category-".$category->category_nicename." #".$category->category_nicename.", ";

  }

 } 

$css_cats .= "{ background: #FFFFFF; color: #232323; text-decoration: none; }";

$css_cats .= "</style>";

echo $css_cats;

}
所以CSS崩溃了

我可以更改代码,使其单独输出CSS声明,如下所示:

.category-wordpress #wordpress { background: #FFFFFF; color: #232323; text-decoration: none; }
.category-wordpress #wordpress, .category-html #html { background: #FFFFFF; color: #232323; text-decoration: none; }
.category-wordpress #wordpress, .category-html #html, { background: #FFFFFF; color: #232323; text-decoration: none; }
.category-wordpress #wordpress { background: #FFFFFF; color: #232323; text-decoration: none; }
.category-html #html { background: #FFFFFF; color: #232323; text-decoration: none; }
但肯定有更好的办法吗

非常感谢您的帮助

干杯


例如,在foreach do之后的James

 $css_cats = substr($css_cats,0,-2);

例如,在foreach do之后

 $css_cats = substr($css_cats,0,-2);
还有一种方法:

$cats_arr = array();

foreach($categories as $category) {
    $cats_arr[] = ".category-".$category->category_nicename." #".$category->category_nicename;  
}

$css_cats .= implode(',', $cats_arr);
参考资料

这里是另一种方式:

$cats_arr = array();

foreach($categories as $category) {
    $cats_arr[] = ".category-".$category->category_nicename." #".$category->category_nicename;  
}

$css_cats .= implode(',', $cats_arr);

参考资料

你是一个传奇人物!这正是我需要的。非常感谢。你是一个传奇人物!这正是我需要的。非常感谢。