Advertisement
Guest User

Untitled

a guest
Sep 1st, 2011
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: DirtySuds - Postlist
  4. Plugin URI: http://dirtysuds.com
  5. Description: Adds shortcode to embed a list of posts
  6. Author: Pat Hawks
  7. Version: 1.00.20110226
  8. Author URI: http://www.pathawks.com
  9.  
  10. Updates:
  11. 1.01 20110323 - Expanded everything
  12. 1.00.20110226 - First Version
  13.  
  14. Copyright 2011 Pat Hawks (email : pat@pathawks.com)
  15.  
  16. This program is free software; you can redistribute it and/or modify
  17. it under the terms of the GNU General Public License as published by
  18. the Free Software Foundation; either version 2 of the License, or
  19. (at your option) any later version.
  20.  
  21. This program is distributed in the hope that it will be useful,
  22. but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. GNU General Public License for more details.
  25.  
  26. You should have received a copy of the GNU General Public License
  27. along with this program; if not, write to the Free Software
  28. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  29. */
  30.  
  31. // [postlist cat="7" showposts="5"]
  32.  
  33. // The shortcode should be able to take almost any argument that WP_Query understands
  34. // For a full list, see http://codex.wordpress.org/Function_Reference/WP_Query#Parameters
  35.  
  36.  
  37. add_shortcode( 'postlist', 'dirtysuds_postlist' );
  38.  
  39. function dirtysuds_postlist( $atts ) {
  40.  
  41. // First, let's lay out some default query parameters
  42. $defaults = array(
  43. 'showposts' => 5,
  44. 'offset' => 0,
  45. 'orderby' => 'post_date',
  46. 'order' => 'DESC',
  47. 'include' => array(),
  48. 'exclude' => array(),
  49. 'meta_key' => '',
  50. 'meta_value' =>'',
  51. 'post_type' => 'post',
  52. );
  53.  
  54.  
  55. // Let's use the array of shortcode attributes as an array of arguments for WP_Query
  56. $query = $atts;
  57. // That's some WTF code
  58.  
  59.  
  60. // If the shortcode included the argument "query" let's parse that first, then merge it with our query defaults
  61. if ($atts['query']) {
  62. $query = wp_parse_args( $atts['query'], $query );
  63. }
  64.  
  65.  
  66. // Finally, run the query
  67. $posts = get_posts($query);
  68.  
  69.  
  70. // Now, to prepare the embeded text to return
  71. $embed = '';
  72.  
  73. if ($posts) {
  74. $embed .= '<ul';
  75. if ($atts['id'])
  76. $embed .= ' id="'.$atts['id'].'"';
  77. $embed .= '>';
  78. foreach( $posts as $post ):
  79. setup_postdata($post);
  80. $embed .= '<li><a href="'.get_permalink($post->ID).'">'.$post->post_title.'</a></li>';
  81. endforeach;
  82.  
  83.  
  84. // If a category has been set, and a "morelink" parameter specified, display a link to that category
  85.  
  86. if ($query['cat'] && $atts['morelink']) {
  87. $embed .= '<li><a href="'.get_category_link($query['cat']).'">'.$atts['morelink'].'</a></li>';
  88. }
  89.  
  90.  
  91. // If a tag has been set, and a "morelink" parameter specified, display a link to that category
  92.  
  93. if ($query['tag_id'] && $atts['morelink']) {
  94. $embed .= '<li><a href="'.get_category_link($query['tag_id']).'">'.$atts['morelink'].'</a></li>';
  95. }
  96.  
  97.  
  98. $embed .= '</ul>';
  99. } else {
  100. $embed = '<!-- No matching posts found -->';
  101. }
  102.  
  103. return $embed;
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement