shoyebzz

Get 1 post from each cat WP

Sep 13th, 2017
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.66 KB | None | 0 0
  1. <?php
  2.         $cat_args = array(
  3.           'orderby' => 'name',
  4.           'order' => 'ASC',
  5.           'child_of' => 0
  6.         );
  7.         $categories=get_categories($cat_args);
  8.         $rand_keys = array_rand($categories, 5); // 5 is the number of categories you want
  9.         foreach ($rand_keys as $key) {
  10.            
  11.             $post_args = array(
  12.               'numberposts' => 1,
  13.               'category' => $categories[$key]->term_id
  14.             );
  15.  
  16.             $posts = get_posts($post_args);
  17.  
  18.             foreach($posts as $post) {
  19.             ?>
  20.                
  21.             content here....
  22.  
  23.  
  24.             <?php
  25.             }
  26.            
  27.         }
  28. ?>
Advertisement
Add Comment
Please, Sign In to add comment