在输入标题中使用Genesis短代码[post_categories]时,我拼命想找到一个解决方案,将条件div类添加到各个类别。
我想将唯一颜色应用于主页和各个帖子上文章的条目标题中的类别标题。 www.mic.com有这种影响。您会注意到" World"类别有一种颜色的文本类别,而#34;新闻"类别将类别文本显示为不同的颜色。
例如,如果类别是"基础设施",我希望有一个包围现有入门类别类的类。
我希望它看起来像这样:
<div class="infrastructure section"><span class="entry-categories"></span></div>
如果类别是&#34;选举&#34;,我希望它显示:
<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>';
关于如何实现这一目标的任何想法?
非常感谢!
答案 0 :(得分: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 );
}