简体   繁体   中英

How to display limited wordpress categories?

I want limit number of categories that are displaying, problem is that it uses not default wordpress (it uses theme code that I have) code aka names that I don't know and google does not found.

So for example this code displays all the categories:

<?php echo cp_create_categories_list( 'dir' ); ?>

How can I limit number of categories that are displaying?

I have tried combine this with default wordpress code:

    <?php
// display 7 random categories
$cats ='';
$categories=get_categories('exclude=' . $GLOBALS[asides_id]);
$rand_keys = array_rand($categories, 7);
foreach ($rand_keys as $key) {
$cats .= $categories[$key]->term_id .',';
}
wp_list_categories('title_li=&include='.$cats);
?>

But I didn't got any success with it maybe because of lack of knowledge or it simply does not work.

Please update your code like this

<?php
// display 7 random categories
$cats ='';
$categories=get_categories('posts_per_page=5&exclude=' . $GLOBALS[asides_id]);
$rand_keys = array_rand($categories, 7);
foreach ($rand_keys as $key) {
$cats .= $categories[$key]->term_id .',';
}
wp_list_categories('title_li=&include='.$cats);
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM