WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] random category list

[Resolved] random category list

Viewing 9 replies - 1 through 9 (of 9 total)
  • <?php
    //display random sorted list of terms in a given taxonomy
    $taxonomy = 'category';
    $terms = get_terms($taxonomy);
    shuffle ($terms);
    //echo 'shuffled';
    if ($terms) {
    	foreach($terms as $term) {
    		echo '<p><a href="' . get_category_link( $term->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $term->name ) . '" ' . '>' . $term->name.'</a></p> ';
    	}
    }
    ?>

    thanks MichaelH 🙂

    but how can i limit the category list because this code show all category i want show only 10 category .

    Put a counter in the foreach loop.

    please give me some more detail because i am newbie in wordpress 🙂

    thanks

    hello MichaelH

    some counter code i searched on google but thet can’t work 🙁
    please help

    <?php
    //display random sorted list of terms in a given taxonomy
    $counter = 0;
    $max = 5; //number of categories to display
    $taxonomy = 'category';
    $terms = get_terms($taxonomy);
    shuffle ($terms);
    //echo 'shuffled';
    if ($terms) {
    	foreach($terms as $term) {
    		$counter++;
    		if ($counter <= $max) {
    	  	echo '<p><a href="' . get_category_link( $term->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $term->name ) . '" ' . '>' . $term->name.'</a></p> ';
    	 	}
    	}
    }
    ?>

    thank you very much Michael 🙂

    I have problems getting this to work with custom taxonomies. The permalink won’t print, and if I change it to “get_term_link” it returns an error. Any help?

    Got it! Swap out get_category_link( $term->term_id ) for get_term_link( $term, $taxonomy )

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘[Resolved] random category list’ is closed to new replies.