Advertisement
ssaidz

Widget Related Post By Kategory

Jun 2nd, 2016
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. function saidz() {
  2. // the query
  3. $the_query = new WP_Query( array( 'category_name' => 'sempak', 'posts_per_page' => 5 ) );
  4. // The Loop
  5. if ( $the_query->have_posts() ) {
  6. $string .= '<ul class="saidz">';
  7. while ( $the_query->have_posts() ) {
  8. $the_query->the_post();
  9. if ( has_post_thumbnail() ) {
  10. $string .= '<li>';
  11. $string .= '<a title="' . get_the_title() .'" href="' . get_the_permalink() .'" >' . get_the_post_thumbnail($post_id, array( 50, 50) ) . get_the_title() .'</a></li>';
  12. } else {
  13. // if no featured image is found
  14. $string .= '<li><a title="' . get_the_title() .'" href="' . get_the_permalink() .'" rel="bookmark">' . get_the_title() .'</a></li>';
  15. }
  16. }
  17. } else {
  18. // belum ada artikel
  19. }
  20. $string .= '</ul>';
  21. return $string;
  22. /* Restore original Post Data */
  23. wp_reset_postdata();
  24. }
  25. // Add a shortcode
  26. add_shortcode('saidz', 'saidz');
  27. // Enable shortcodes in text widgets
  28. add_filter('widget_text', 'do_shortcode');
  29.  
  30. ** Ganti Kata 'sempak' dengan nama kategori yang akan di tampilkan.
  31. ** Taruh Script Di Atas di function.php
  32. ** kemudian panggil di dalam widget menggunakan shortcode [saidz]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement