Want more features on Pastebin? Sign Up, it's FREE!
Guest

list_cat_posts.php

By: a guest on Jan 17th, 2011  |  syntax: PHP  |  size: 6.61 KB  |  views: 547  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2. /*
  3. Plugin Name: List category posts
  4. Plugin URI: http://picandocodigo.net/programacion/wordpress/list-category-posts-wordpress-plugin-english/
  5. Description: List Category Posts allows you to list posts from a category into a post/page using the [catlist] shortcode. This shortcode accepts a category name or id, the order in which you want the posts to display, and the number of posts to display. You can use [catlist] as many times as needed with different arguments. Usage: [catlist argument1=value1 argument2=value2].
  6. Version: 0.14.1
  7. Author: Fernando Briano
  8. Author URI: http://picandocodigo.net/
  9. */
  10.  
  11. /* Copyright 2008-2011  Fernando Briano  (email : fernando@picandocodigo.net)
  12.  
  13. This program is free software; you can redistribute it and/or modify
  14. it under the terms of the GNU General Public License as published by
  15. the Free Software Foundation; either version 3 of the License, or
  16. any later version.
  17.  
  18. This program is distributed in the hope that it will be useful,
  19. but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21. GNU General Public License for more details.
  22.  
  23. You should have received a copy of the GNU General Public License
  24. along with this program; if not, write to the Free Software
  25. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  26. */
  27.  
  28.  
  29. include('list_cat_posts_widget.php');
  30.  
  31. /**
  32.  *
  33.  * Main plugin function: Gets the shortcode parameters, set defaults, and call the plugin's function.
  34.  * @param $atts
  35.  * @param $content
  36.  */
  37. function catlist_func($atts, $content = null) {
  38.         $atts = shortcode_atts(array(
  39.                         'id' => '0',
  40.                         'name' => 'default',
  41.                         'orderby' => 'date',
  42.                         'order' => 'desc',
  43.                         'numberposts' => '5',
  44.                         'date' => 'no',
  45.                         'author' => 'no',
  46.                         'dateformat' => get_option('date_format'),
  47.                         'template' => 'default',
  48.                         'excerpt' => 'no',
  49.                         'exclude' => '0',
  50.                         'excludeposts' => '0',
  51.                         'offset' => '0',
  52.                         'tags' => '',
  53.                         'content' => 'no',
  54.                         'catlink' => 'no',
  55.                         'comments' => 'no',
  56.                         'thumbnail' => 'no',
  57.                         'post_type' => '',
  58.                         'post_parent' => '0',
  59.                         'class' => 'lcp_catlist'
  60.                 ), $atts);
  61.         return list_category_posts($atts);
  62. }
  63.  
  64. /* Add the shortcode to WordPress */
  65. add_shortcode('catlist', 'catlist_func');
  66.  
  67. /**
  68.  * Main function, this is where the flow goes and calls auxiliary functions
  69.  * @param array $atts
  70.  */
  71. function list_category_posts($atts){
  72.         $lcp_category_id = $atts['id'];
  73.         $lcp_category_name = $atts['name'];
  74.        
  75.         //Get the category posts:
  76.         $catposts = lcp_category($lcp_category_id, $lcp_category_name, $atts);
  77.        
  78.         $lcp_output = '';
  79.        
  80.         //Link to the category:
  81.         if ($atts['catlink'] == 'yes'){
  82.                 $cat_link = get_category_link($lcp_category_id);
  83.                 $cat_title = get_cat_name($lcp_category_id);
  84.                 $lcp_output .= '<a href="' . $cat_link . '" title="' . $cat_title . '">' . $cat_title . '</a>';
  85.         }
  86.        
  87.         //Template code:
  88.         $tplFileName = null;
  89.         $possibleTemplates = array(
  90.                 // File locations lower in list override others
  91.                 WP_PLUGIN_DIR.'/list-category-posts/templates/'.$atts['template'].'.php',
  92.         );
  93.         foreach ($possibleTemplates as $key => $file) {
  94.                 if (is_readable($file)) {
  95.                         $tplFileName = $file;
  96.                 }
  97.         }
  98.         if ((!empty($tplFileName)) && (is_readable($tplFileName))) {
  99.                 require($tplFileName);
  100.         }else{
  101.                 // Default template
  102.                 $lcp_output .= '<ul class="'.$atts['class'].'">';
  103.                
  104.                 foreach ($catposts as $single):
  105.                         $lcp_output .= lcp_display_post($single, $atts);
  106.                 endforeach;
  107.                
  108.                 $lcp_output .= "</ul>";
  109.         }
  110.         return $lcp_output;
  111. }
  112.  
  113. /**
  114.  * Get the categories
  115.  * @param string $lcp_category_id
  116.  * @param string $lcp_category_name
  117.  */
  118. function lcp_category($lcp_category_id, $lcp_category_name, $atts){
  119.         if($lcp_category_name != 'default' && $lcp_category_id == '0'){
  120.                 $lcp_category = 'category_name=' . $atts['name'];
  121.                 $category_id = get_cat_ID($atts['name']);
  122.         }else{
  123.                 $lcp_category = 'cat=' . $atts['id'];
  124.                 $category_id = $atts['id'];
  125.         }
  126.  
  127.         //Build the query for get_posts()
  128.         $lcp_query = $lcp_category.'&numberposts=' . $atts['numberposts'] .
  129.                                 '&orderby=' . $atts['orderby'] .
  130.                                 '&order=' . $atts['order'] .
  131.                                 '&exclude=' . $atts['excludeposts'] .
  132.                                 '&tag=' . $atts['tags'] .
  133.                                 '&offset=' . $atts['offset'];
  134.         if($atts['post_type']): $lcp_query .= '&post_type=' . $atts['post_type']; endif;
  135.         if($atts['post_parent']): $lcp_query .= '&post_parent=' . $atts['post_parent']; endif;
  136.  
  137.         return get_posts($lcp_query);
  138. }
  139.  
  140. function lcp_display_post($single, $atts){
  141.         $lcp_output .= '<li><a href="' . get_permalink($single->ID).'">' . $single->post_title . '</a>';
  142.        
  143.         if ($atts['comments'] == yes){
  144.                 $lcp_output .= ' (';
  145.                 $lcp_output .=  lcp_comments($single);
  146.                 $lcp_output .=  ')';
  147.         }
  148.        
  149.         if ($atts['date']=='yes'){
  150.                 $lcp_output .= lcp_showdate($single, $atts['dateformat']);
  151.         }
  152.        
  153.         if ($atts['author']=='yes'){
  154.                 $lcp_output .= " - ".lcp_showauthor($single) . '<br/>';
  155.         }
  156.        
  157.         if ($atts['content']=='yes' && $single->post_content){
  158.                 $lcp_output.= lcp_content($single); // line tweaked to output filtered content
  159.         }
  160.        
  161.         if ($atts['excerpt']!='no' && !($atts['content']=='yes' && $single->post_content) ){
  162.                 $lcp_output .= lcp_excerpt($single);
  163.         }
  164.        
  165.         if ($atts['thumbnail']=='yes'){
  166.                 $lcp_output .= lcp_thumbnail($single);
  167.         }
  168.         $lcp_output.="</li>";
  169.         return $lcp_output;
  170. }
  171.  
  172. function lcp_comments($single){
  173.         return $single->comment_count;
  174. }
  175.  
  176. function lcp_showauthor($single){
  177.         $lcp_userdata = get_userdata($single->post_author);
  178.         return $lcp_userdata->display_name;
  179. }
  180.  
  181. function lcp_showdate($single, $dateformat){
  182.         return  ' - ' . get_the_time($dateformat, $single);//by Verex, great idea!
  183. }
  184.  
  185. function lcp_content($single){
  186.         $lcp_content = apply_filters('the_content', $single->post_content); // added to parse shortcodes
  187.         $lcp_content = str_replace(']]>', ']]&gt', $lcp_content); // added to parse shortcodes
  188.         return '<p>' . $lcp_content . '</p>';
  189. }
  190.  
  191.  
  192. function lcp_excerpt($single){
  193.         if($single->post_excerpt){
  194.                 return '<p>' . $single->post_excerpt . '</p>';
  195.         }
  196.         $lcp_excerpt = strip_tags($single->post_content);
  197.         if ( post_password_required($post) ) {
  198.                 $lcp_excerpt = __('There is no excerpt because this is a protected post.');
  199.                 return $lcp_excerpt;
  200.         }
  201.         if (strlen($lcp_excerpt) > 255) {
  202.                 $lcp_excerpt = substr($lcp_excerpt, 0, 252) . '...';
  203.         }
  204.         return '<p>' . $lcp_excerpt . '</p>';
  205. }
  206.  
  207.  
  208. function lcp_thumbnail($single){
  209.         $lcp_thumbnail = '';
  210.         if ( has_post_thumbnail($single->ID) ) {
  211.                 $lcp_thumbnail = get_the_post_thumbnail($single->ID);
  212.         }
  213.         return $lcp_thumbnail;
  214. }
  215.  
  216. /** TODO - These are the todo's for a 1.0 release:
  217.  *  -Pagination
  218.  *  -Simplify template system
  219.  *  -i18n
  220.  */
  221. ?>
clone this paste RAW Paste Data