WordPress - 高级自定义功能wp_list_categories

时间:2013-10-22 12:47:28

标签: php wordpress

使用WordPress功能“ wp_list_categories ”时:

<?php wp_list_categories('orderby=name&show_count=1&depth=2&exclude=1,1148'); ?>

结果:

<li class="cat-item">
<a href="x" title="x">Cat Name</a> (Cat Count)
</li>

我的目标:

<h2>
<a href="x" title="x" ><img src="Category_Slug.png" alt="x"/> Cat Name</a> (Cat Count)
</h2><hr />

如何修改此功能以获取我的定位结果?

2 个答案:

答案 0 :(得分:0)

您可以使用类似的内容来完全控制您的类别列表

global $wpdb;
$taxonomy = CUSTOM_CAT_TYPE; // in your case, it's categories
$table_prefix = $wpdb->prefix;
$wpcat_id = NULL;

//Fetch category                          
$wpcategories = (array) $wpdb->get_results("
    SELECT * FROM {$table_prefix}terms, {$table_prefix}term_taxonomy
    WHERE {$table_prefix}terms.term_id = {$table_prefix}term_taxonomy.term_id
    AND {$table_prefix}term_taxonomy.taxonomy ='" . $taxonomy . "' and  {$table_prefix}term_taxonomy.parent=0  
    ORDER BY {$table_prefix}terms.name ASC" ); 

foreach ($wpcategories as $wpcat) { 
    $name = $wpcat->name;
    echo '<a href="'.$wpcat->slug.'"><img src="'.$wpcat->slug.'.png"/></a>';
}

如果你有父类别和儿童类别,你可以做这样的事情

foreach ($wpcategories as $wpcat) { 
    $name = $wpcat->name;
    $termid = $wpcat->term_id; //parent category ID

    $args = array(
      'type' => POST_TYPE, //in normal case its post otherwise custom pist type
       'child_of' => '',
       'parent' => $termid, //parent category ID
       'orderby' => 'name',
       'order' => 'ASC',
       'hide_empty' => 0,
       'hierarchical' => 1,
       'exclude' => $termid, //stop duplicate parent category
       'include' => '',
       'number' => '',
       'taxonomy' => CUSTOM_CAT_TYPE, //categories in ur case
       'pad_counts' => false);

    $acb_cat = get_categories($args); //ALSO can SIMPLY USE this LINE and previous arguments
                    //Code for showing number of posts from subcategories you can do it for categories as you have get_categories output.
                        $postCount = 0;   
                            foreach($acb_cat as $subcat)
                            {                                    
                                $countposts = get_term($subcat->term_id,$taxonomy);

                                $postCount += $countposts->count;
                            }
    echo '<a href="'.$wpcat->slug.'"><img src="'.$wpcat->slug.'.png"/></a>'.$postCount;
}

答案 1 :(得分:0)

你也可以在这个主题的function.php中添加新功能。

  add_filter ( 'wp_list_categories', 'img_before_link_list_categories' );

    function img_before_link_list_categories( $list ) {
      $cats = get_categories();
        foreach($cats as $cat) {

            $find = $cat->name.'</a>';
            $replace = '<img src="'.$cat->slug.'.png" alt="x"/>'.$cat->name.'</a>';
            $list = str_replace( $find, $replace, $list );

            $list = preg_replace('%<li class=".*">|</?ul>%U', '<h2>', $list);
            $list = str_replace('</li>', '</h2>', $list);
        }
     return $list;
    }