2014-07-04 21 views
1

我拼命試圖找到一種解決方案,以在條目標題中使用Genesis簡碼[post_categories]時向各個類別添加條件div類。將條件div類添加到創世紀簡碼條目標題

我想在主頁和各個帖子上的文章的條目標題中的類別標題上應用唯一顏色。 www.mic.com有這種影響。您會注意到,「世界」類別下的文章具有一種顏色的類別文本,而「新聞」類別下的文章將類別文本顯示爲不同的顏色。

例如,如果類別是「基礎結構」,我希望有一個類包裝現有的入門類類。

我希望它看起來像這樣:

<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功能有條件創建的類名試過,但該命令不能完成的動作。當通過螢火蟲查看我的網站時,新班級將該命令顯示爲文本。

$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

像這樣的東西應該工作(但僅限於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); 

}