LOS MAS VISTOS, FUNCTION WORDPRESS


/ Published in: PHP
Save to your folder(s)

Funcion los post más vistos en wordpress.


Copy this code and paste it in your HTML
  1. function get_most_viewed($mode = '', $limit = 10, $chars = 0, $display = true) {
  2. global $wpdb;
  3. $views_options = get_option('views_options');
  4. $where = '';
  5. $temp = '';
  6. $output = '';
  7. if(!empty($mode) && $mode != 'both') {
  8. $where = "post_type = '$mode'";
  9. } else {
  10. $where = '1=1';
  11. }
  12. $most_viewed = $wpdb->get_results("SELECT DISTINCT $wpdb->posts.*, (meta_value+0) AS views FROM $wpdb->posts LEFT JOIN $wpdb->postmeta ON $wpdb->postmeta.post_id = $wpdb->posts.ID WHERE post_date < '".current_time('mysql')."' AND $where AND post_status = 'publish' AND meta_key = 'views' AND post_password = '' ORDER BY views DESC LIMIT $limit");
  13. if($most_viewed) {
  14. foreach ($most_viewed as $post) {
  15. $post_views = intval($post->views);
  16. $post_title = get_the_title($post);
  17. if($chars > 0) {
  18. $post_title = snippet_text($post_title, $chars);
  19. }
  20. $post_excerpt = views_post_excerpt($post->post_excerpt, $post->post_content, $post->post_password, $chars);
  21. $temp = stripslashes($views_options['most_viewed_template']);
  22. $temp = str_replace("%VIEW_COUNT%", number_format_i18n($post_views), $temp);
  23. $temp = str_replace("%POST_TITLE%", $post_title, $temp);
  24. $temp = str_replace("%POST_EXCERPT%", $post_excerpt, $temp);
  25. $temp = str_replace("%POST_CONTENT%", $post->post_content, $temp);
  26. $temp = str_replace("%POST_URL%", get_permalink($post), $temp);
  27. $output .= $temp;
  28. }
  29. } else {
  30. $output = '<li>'.__('N/A', 'wp-postviews').'</li>'."\n";
  31. }
  32. if($display) {
  33. echo $output;
  34. } else {
  35. return $output;
  36. }
  37. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.