首页 文章

将条件div类添加到Genesis短代码入口 Headers

提问于
浏览
1

当我在entry-header中使用Genesis短代码[post_categories]时,我拼命想找到一个解决方案来为各个类别添加一个条件div类 .

我想在主页和各个帖子的文章的条目 Headers 中为类别 Headers 应用唯一的颜色 . 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>';

有关如何实现这一目标的任何想法?

非常感谢!

1 回答

  • 0

    这样的东西应该工作(但仅适用于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 );
    
    }
    

相关问题