WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] random category list (10 posts)

  1. asifg123
    Member
    Posted 4 years ago #

    Hi

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

    What do I do?

    thanks

  2. MichaelH
    Member
    Posted 4 years ago #

    <?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> ';
    	}
    }
    ?>
  3. asifg123
    Member
    Posted 4 years ago #

    thanks MichaelH :)

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

  4. MichaelH
    Member
    Posted 4 years ago #

    Put a counter in the foreach loop.

  5. asifg123
    Member
    Posted 4 years ago #

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

    thanks

  6. asifg123
    Member
    Posted 4 years ago #

    hello MichaelH

    some counter code i searched on google but thet can't work :(
    please help

  7. MichaelH
    Member
    Posted 4 years ago #

    <?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> ';
    	 	}
    	}
    }
    ?>
  8. asifg123
    Member
    Posted 4 years ago #

    thank you very much Michael :)

  9. RachelNabors
    Member
    Posted 3 years ago #

    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?

  10. RachelNabors
    Member
    Posted 3 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic