Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将条件div类添加到Genesis短代码条目头_Php_Css_Shortcode_Genesis - Fatal编程技术网

Php 将条件div类添加到Genesis短代码条目头

Php 将条件div类添加到Genesis短代码条目头,php,css,shortcode,genesis,Php,Css,Shortcode,Genesis,当在条目标题中使用Genesis shortcode[post_categories]时,我正在拼命地寻找一个解决方案,将一个条件div类添加到各个类别中 我想在主页和个人帖子的条目标题中为类别标题应用独特的颜色。www.mic.com有这种影响。您将注意到,“世界”类别下的文章将“类别文本”显示为一种颜色,而“新闻”类别下的文章将“类别文本”显示为另一种颜色 例如,如果类别是Infrastructure,我希望有一个类围绕现有的entry categories类 我希望它看起来像这样: <

当在条目标题中使用Genesis shortcode[post_categories]时,我正在拼命地寻找一个解决方案,将一个条件div类添加到各个类别中

我想在主页和个人帖子的条目标题中为类别标题应用独特的颜色。www.mic.com有这种影响。您将注意到,“世界”类别下的文章将“类别文本”显示为一种颜色,而“新闻”类别下的文章将“类别文本”显示为另一种颜色

例如,如果类别是Infrastructure,我希望有一个类围绕现有的entry categories类

我希望它看起来像这样:

<div class="infrastructure section"><span class="entry-categories"></span></div>
如果类别为选举,我希望它显示:

<div class="elections section"><span class="entry-categories"></span></div>
下面列出了我需要编辑以获得此效果的代码

add_shortcode( 'post_categories', 'genesis_post_categories_shortcode' );
/**
 * Produces the category links list.
 *
 * Supported shortcode attributes are:
 *   after (output after link, default is empty string),
 *   before (output before link, default is 'Tagged With: '),
 *   sep (separator string between tags, default is ', ').
 *
 * Output passes through 'genesis_post_categories_shortcode' filter before returning.
 *
 * @since 1.1.0
 *
 * @param array|string $atts Shortcode attributes. Empty string if no attributes.
 * @return string Shortcode output
 */
function genesis_post_categories_shortcode( $atts ) {

$defaults = array(
    'sep'    => ', ',
    'before' => __( 'Filed Under: ', 'genesis' ),
    'after'  => '',
);

$atts = shortcode_atts( $defaults, $atts, 'post_categories' );

$cats = get_the_category_list( trim( $atts['sep'] ) . ' ' );

if ( genesis_html5() )
    $output = sprintf( '<span %s>', genesis_attr( 'entry-categories' ) ) . $atts['before'] . $cats . $atts['after'] . '</span>';
else
    $output = '<span class="categories">' . $atts['before'] . $cats . $atts['after'] . '</span>';

return apply_filters( 'genesis_post_categories_shortcode', $output, $atts );

}
我尝试使用get_the_category_list函数有条件地创建类名,但该命令没有完成操作。通过firebug查看我的站点时,新类将命令显示为文本

$cats = get_the_category_list( trim( $atts['sep'] ) . ' ' );

if ( genesis_html5() )
    $output = sprintf( '<div class="<?php echo $cats[0]->cat_name; ?> section"><span %s>', genesis_attr( 'entry-categories' ) ) . $atts['before'] . $cats . $atts['after'] . '</span></div>';
关于如何实现这种效果有什么想法吗


非常感谢

类似的功能应该可以使用,但仅适用于html5:

function custom_terms_shortcode( $atts ) {

    $defaults = array(
                    'after'    => '',
                    'before'   => __( 'Filed Under: ', 'genesis' ),
                    'sep'      => ', ',
                    'taxonomy' => 'category',
    );

    $atts = shortcode_atts( $defaults, $atts, 'custom_terms' );

    $terms = get_the_terms( get_the_ID(), $atts['taxonomy'] );

    if ( is_wp_error( $terms ) )
                    return;

    if ( empty( $terms ) )
                    return;

    if ( genesis_html5() ){


            $output .= '<span class="entry-terms">';
            $output .= $atts['before'];
            foreach ($terms as $term) {
            $output = '<div class="'. $term->name .'">';
            $output .= '<a href="'.esc_attr(add_query_arg( $atts['taxonomy'], $term->slug ),'').'">'.$term->name.'</a>'.$atts['sep'];
            $output .= '</div>';
            }
            $output = substr($output, 0, -2);
            $output .= $atts['after'];
            $output .= '</span>';
    }

    return apply_filters( 'genesis_post_terms_shortcode', $output, $terms, $atts );

}