LOS MAS VISTOS POR CATEGORIA, WORDPRESS


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



Copy this code and paste it in your HTML
  1. function get_most_viewed_category($category_id = 0, $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(is_array($category_id)) {
  8. $category_sql = "$wpdb->term_taxonomy.term_id IN (".join(',', $category_id).')';
  9. } else {
  10. $category_sql = "$wpdb->term_taxonomy.term_id = $category_id";
  11. }
  12. if(!empty($mode) && $mode != 'both') {
  13. $where = "post_type = '$mode'";
  14. } else {
  15. $where = '1=1';
  16. }
  17. $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 INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) WHERE post_date < '".current_time('mysql')."' AND $wpdb->term_taxonomy.taxonomy = 'category' AND $category_sql AND $where AND post_status = 'publish' AND meta_key = 'views' AND post_password = '' ORDER BY views DESC LIMIT $limit");
  18. if($most_viewed) {
  19. foreach ($most_viewed as $post) {
  20. $post_views = intval($post->views);
  21. $post_title = get_the_title($post);
  22. if($chars > 0) {
  23. $post_title = snippet_text($post_title, $chars);
  24. }
  25. $post_excerpt = views_post_excerpt($post->post_excerpt, $post->post_content, $post->post_password, $chars);
  26. $temp = stripslashes($views_options['most_viewed_template']);
  27. $temp = str_replace("%VIEW_COUNT%", number_format_i18n($post_views), $temp);
  28. $temp = str_replace("%POST_TITLE%", $post_title, $temp);
  29. $temp = str_replace("%POST_EXCERPT%", $post_excerpt, $temp);
  30. $temp = str_replace("%POST_CONTENT%", $post->post_content, $temp);
  31. $temp = str_replace("%POST_URL%", get_permalink($post), $temp);
  32. $output .= $temp;
  33. }
  34. } else {
  35. $output = '<li>'.__('N/A', 'wp-postviews').'</li>'."\n";
  36. }
  37. if($display) {
  38. echo $output;
  39. } else {
  40. return $output;
  41. }
  42. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.