• Resolved asifg123

    (@asifg123)


    Hi

    is there any way that i can show random category list in my post bottom side

    What do I do?

    thanks

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> ';
    	}
    }
    ?>
    Thread Starter asifg123

    (@asifg123)

    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.

    Thread Starter asifg123

    (@asifg123)

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

    thanks

    Thread Starter asifg123

    (@asifg123)

    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> ';
    	 	}
    	}
    }
    ?>
    Thread Starter asifg123

    (@asifg123)

    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 ‘random category list’ is closed to new replies.