色々と検索していたら、このコードを見つけました。
ほぼほぼやりたいことはできたのですが、
ここから任意の件数分表示したい場合どのようにしたらいいでしょうか?
<?php
function nestmenu( $args ) {
$nestmenu = '';
if ( ! isset( $args['exclude'] ) ) {
$args['exclude'] = '';
}
$opt = array(
'hide_empty' => true,
'exclude_tree' => $args['exclude']
);
$cat_array = get_categories( $opt );
$nestmenu = "<div id='nestmenu'>\n";
$nestmenu .= "<ul class='nestmenu-category'>\n";
foreach ( $cat_array as $key => $cat ) {
$cat_name = $cat->name;
$cat_num = $cat->count;
$cat_link = get_category_link( $cat->term_id );
$tag_array = nestmenu_relation($cat->term_id);
$nestmenu .= "<li><h2><a href='$cat_link' title='$cat_name'>$cat_name ($cat_num)</a></h2></li>\n";
$nestmenu .= "<ul class='nestmenu-tag'>\n";
foreach ( $tag_array as $tags ) {
foreach ( $tags as $tag ) {
$tag_link = esc_url( home_url( '/' ) . '?cat=' . $cat->term_id . '&tag=' . $tag['slug'] );
$nestmenu .= "<li><a href='$tag_link' title='$tag[name]'>$tag[name] ($tag[count])</a></li>";
}
}
$nestmenu .= "</ul>\n";
}
$nestmenu .= "</ul>\n";
$nestmenu .= "</div>\n";
return $nestmenu;
}
add_shortcode( 'nestmenu', 'nestmenu' );
add_filter( 'widget_text', 'do_shortcode' );
function nestmenu_relation( $term_id = false ) {
if ( $term_id === false ) {
return $term_ids;
}
$taxonomy = get_terms(
array(
'category',
'post_tag'
),
array(
'hide_empty' => true,
'include' => $term_id
)
);
$taxonomy = $taxonomy[0]->taxonomy;
if ( ! is_null( $taxonomy ) ) {
$args = array(
'nopaging' => 1,
'ignore_sticky_posts' => 1,
'order' => 'ASK',
'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'terms' => $term_id,
'field' => 'id',
'operator' => 'AND'
)
)
);
$my_query = new WP_Query( $args );
if( $my_query->have_posts() ) {
if ( $taxonomy === 'category' ) {
$taxonomy = 'post_tag';
} else {
$taxonomy = 'category';
}
while ( $my_query->have_posts() ) : $my_query->the_post();
$term = get_the_terms( get_the_ID(), $taxonomy );
if ( $term ) {
foreach ( $term as $value ) {
if ( ! isset( $term_ids['term'][$value->term_id] ) ) {
$term_info = array(
'id' => $value->term_id,
'name' => $value->name,
'slug' => $value->slug,
);
$term_ids['term'][$value->term_id] = $term_info;
}
$term_ids['term'][$value->term_id]['count'] += 1;
}
}
endwhile;
wp_reset_query();
}
if ( $term_ids['term'] ) {
$term_ids['term'] = array_merge( $term_ids['term'] );
foreach ( $term_ids['term'] as $key => $value ) {
$key_id[$key] = $value['count'];
}
array_multisort( $key_id, SORT_DESC, $term_ids['term'] );
}
}
return $term_ids;
}
?>