Php Wordpress主题:仅显示一个类别
我的WordPress主题,显示一篇文章在主页上的所有类别,我只想显示一个类别,即使一篇文章在多个类别下 这是我的主题中的代码:Php Wordpress主题:仅显示一个类别,php,wordpress,wordpress-theming,Php,Wordpress,Wordpress Theming,我的WordPress主题,显示一篇文章在主页上的所有类别,我只想显示一个类别,即使一篇文章在多个类别下 这是我的主题中的代码: function metro_magazine_colored_category(){ $output = ''; // Hide category for pages. if ( 'post' === get_post_type() ) { $categories_list = get_the_category(); if ( $categ
function metro_magazine_colored_category(){
$output = '';
// Hide category for pages.
if ( 'post' === get_post_type() ) {
$categories_list = get_the_category();
if ( $categories_list ) {
$output .= '<div class="category-holder">';
foreach( $categories_list as $category ){
$color_code = get_theme_mod( 'metro_magazine_category_color_' . $category->term_id );
if ( $color_code ) {
$output .= '<a class="category" href="' . esc_url( get_category_link( $category->term_id ) ) . '" style="background:' . esc_attr( $color_code ) . '" rel="category tag">'. esc_html( $category->cat_name ) .'</a>';
}else{
$output .= '<a class="category" href="' . esc_url( get_category_link( $category->term_id ) ) . '" rel="category tag">' . esc_html( $category->cat_name ) . '</a>';
}
}
$output .= '</div>';
echo $output;
}
}e
}
endif;
功能地铁杂志彩色分类(){
$output='';
//隐藏页面的类别。
如果('post'==get_post_type()){
$categories_list=获取_类别();
如果($categories\u list){
$output.='';
foreach($categories\u作为$categories列出){
$color\u code=get\u theme\u mod('metro\u magazine\u category\u color'.$category->term\u id);
if($color\U代码){
$output.='';
}否则{
$output.='';
}
}
$output.='';
echo$输出;
}
}e
}
endif;
以下代码可能会对您有所帮助。您不需要foreach循环,因为您希望只打印第一个类别。希望能有帮助
function metro_magazine_colored_category(){
$output = '';
// Hide category for pages.
if ( 'post' === get_post_type() ) {
$categories_list = get_the_category();
if ( $categories_list ) {
$output .= '<div class="category-holder">';
$color_code = get_theme_mod( 'metro_magazine_category_color_' . $categories_list[0]->term_id );
if ( $color_code ) {
$output .= '<a class="category" href="' . esc_url( get_category_link( $category[0]->term_id ) ) . '" style="background:' . esc_attr( $color_code ) . '" rel="category tag">'. esc_html( $categories_list[0]->cat_name ) .'</a>';
}else{
$output .= '<a class="category" href="' . esc_url( get_category_link( $categories_list[0]->term_id ) ) . '" rel="category tag">' . esc_html( $categories_list[0]->cat_name ) . '</a>';
}
$output .= '</div>';
echo $output;
}
}
}
功能地铁杂志彩色分类(){
$output='';
//隐藏页面的类别。
如果('post'==get_post_type()){
$categories_list=获取_类别();
如果($categories\u list){
$output.='';
$color\U code=get\u theme\u mod('metro\u magazine\u category\u color'.$categories\u list[0]->term\u id);
if($color\U代码){
$output.='';
}否则{
$output.='';
}
$output.='';
echo$输出;
}
}
}
是否只需要显示第一个类别?是的,第一个类别可以。请检查答案。请让我知道它是否适合你。谢谢Tristup,但它现在没有显示任何类别好的,我正在检查它。