0

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 />

この関数を変更してターゲットの結果を取得するにはどうすればよいですか?

4

2 に答える 2

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;
}
于 2013-10-22T13:09:29.593 に答える