Advertisement
pusatdata

Menampilkan Random Post di Page >> Bisa untuk Home

Nov 5th, 2017
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. Menampilkan Random Post di Page >> Bisa untuk Home
  2.  
  3. Kode untuk functions.php:
  4.  
  5. function wpb_rand_posts() {
  6.  
  7. $args = array(
  8. 'post_type' => 'post',
  9. 'orderby' => 'rand',
  10. 'posts_per_page' => 5,
  11. );
  12.  
  13. $the_query = new WP_Query( $args );
  14.  
  15. if ( $the_query->have_posts() ) {
  16.  
  17. $string .= '<ul>';
  18. while ( $the_query->have_posts() ) {
  19. $the_query->the_post();
  20. $string .= '<li><a href="'. get_permalink() .'">'. get_the_title() .'</a></li>';
  21. }
  22. $string .= '</ul>';
  23. /* Restore original Post Data */
  24. wp_reset_postdata();
  25. } else {
  26.  
  27. $string .= 'no posts found';
  28. }
  29.  
  30. return $string;
  31. }
  32.  
  33. add_shortcode('wpb-random-posts','wpb_rand_posts');
  34. add_filter('widget_text', 'do_shortcode');
  35.  
  36. Kode di post atau page:
  37. [wpb-random-posts]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement