Advertisement
Guest User

WordPress ChildPages plugin

a guest
Sep 3rd, 2013
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.59 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Bryans Childpage Thumbnail Generator
  4. Plugin URI: http://www.bryanleister.com/projects/childpages-plugin/
  5. Description: A plug-in that creates child page thumbnails with or without titles. Shortcode is [gallery_childpages]. Place that on a parent page and all the children will be listed. Options include styling and the ability to place on pages that are not the parent by using the id of a parent page. All options: [gallery_childpages order="ASC" orderby="title" id="1290" number="-1" height="100" width="200" size="thumbnail" margin="10px 0 0 20px" style="my_style" include="" pagetitle="1" selector="H2" selector_height="30px" selector_width"100%" selector_padding="10px 0 0 20px" showimages="0" showimages="true" exclude="1,33,22"] . Options follow wordpress conventions, however I am use 0 for 'false' and 1 for 'true' in showimages and pagetitle.
  6. Version: 1.2
  7. Author: Bryan Leister
  8. Author URI: http://bryanleister.com
  9. License: GPL2
  10. */
  11.  
  12. function endsWith($haystack, $needle)
  13. {
  14. return $needle === "" || substr($haystack, -strlen($needle)) === $needle;
  15. }
  16.  
  17. function current_page_url() {
  18. $pageURL = 'http';
  19. if( isset($_SERVER["HTTPS"]) ) {
  20. if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
  21. }
  22. $pageURL .= "://";
  23. if ($_SERVER["SERVER_PORT"] != "80") {
  24. $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
  25. } else {
  26. $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
  27. }
  28. return $pageURL;
  29. }
  30.  
  31.  
  32. add_shortcode('gallery_childpages', 'gallery_childpages_shortcode');
  33.  
  34. /* Child Pages code
  35. Based on the Gallery code we are finding all of the Children of a Page and then displaying
  36. them as either a thumbnail or as an image thumbnail.
  37. */
  38. function gallery_childpages_shortcode($attr)
  39. {
  40.  
  41. global $post, $wp_locale;
  42.  
  43. static $instance = 0;
  44. $instance++;
  45. $childpages = array();
  46.  
  47. // Allow plugins/themes to override the default gallery template.
  48. $output = apply_filters('post_gallery', '', $attr);
  49. if ( $output != '' )
  50. {
  51. return $output;
  52. }
  53.  
  54. // We're trusting author input, so let's at least make sure it looks like a valid orderby statement
  55. if ( isset( $attr['orderby'] ) )
  56. {
  57. $attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
  58. if ( !$attr['orderby'] )
  59. {
  60. unset( $attr['orderby'] );
  61. }
  62. }
  63.  
  64. extract(shortcode_atts(array(
  65. 'order' => 'ASC',
  66. 'orderby' => 'title',
  67. 'id' => '',
  68. 'number' => -1,
  69. 'height' => '100',
  70. 'width' => '',
  71. 'size' => 'thumbnail',
  72. 'style' => '',
  73. 'include' => '',
  74. 'pagetitle' => false,
  75. 'selector' => 'h2',
  76. 'selector_height' => '30px',
  77. 'selector_width' => '100%',
  78. 'selector_padding' => '',
  79. 'showimages' => true,
  80. 'text_align' => 'left',
  81. 'text_margin' => '',
  82. 'exclude' => '',
  83. 'posts_per_page' => '5',
  84. 'page_number' => '1'
  85. ), $attr));
  86.  
  87. if( $id == '')
  88. {
  89. $id = $post->ID;
  90. }
  91.  
  92. $id = intval($id);
  93.  
  94. if($width != null)
  95. {
  96. $width='width:' . $width . 'px';
  97. }
  98. // $width="width:140px";
  99.  
  100. if ($order == 'RAND')
  101. {
  102. $orderby = 'none';
  103. }
  104.  
  105. if($text_margin == '')
  106. {
  107. $textmargin = ($height/2)-10;
  108. $textmargin .= "px 0 0 0";
  109.  
  110. } else
  111. {
  112. $textmargin = $text_margin;
  113. }
  114. // $textmargin = ($height)-30;
  115. $offsettext = ($height*3);
  116.  
  117. $offset = ($page_number - 1) * $posts_per_page;
  118.  
  119. $countposts = count(get_posts('post_parent=' . $id . '&post_type=page&post_status=publish&numberposts=' . $number . '&exclude=' . $exclude . '&orderby=' . $orderby));
  120.  
  121. $totalpages = $countposts / $posts_per_page;
  122. $totalpages = intval($totalpages) + ($totalpages > intval($totalpages) ? 1 : 0);
  123.  
  124. $childpages = & get_posts('post_parent=' . $id . '&post_type=page&post_status=publish&numberposts=' . $number . '&exclude=' . $exclude . '&orderby=' . $orderby . '&order=' . $order . '&posts_per_page=' . $posts_per_page . '&offset=' . $offset);
  125. //
  126.  
  127. //Check if we have anything to output
  128. if ( empty($childpages))
  129. {
  130. $output = "<p>There are no child pages for post id # " . $id . "</p>";
  131.  
  132. } else //We do have childpages, let's loop through them
  133. {
  134.  
  135. $current_url = current_page_url();
  136. $baseurl = endsWith($current_url, $post->post_name) ? $current_url . "/" : "";
  137. $pagination = "<div class='pagination'>";
  138. for($i=1; $i <= $totalpages; ++$i)
  139. if($i == 1 && $page_number == 0)
  140. $pagination .= "<a class='page_number active' href='" . $baseurl . "1/'>1</a>";
  141. else
  142. $pagination .= "<a class='page_number" . ($page_number == $i ? " active" : "") . "' href='" . $baseurl . $i . "/'>" . $i . "</a>";
  143. $pagination .= "</div>";
  144.  
  145. //Nest the gallery inside a DIV to prevent Wordpress from adding a <p> to the post and screwing it up...
  146. $output = "<div class='clear'>";
  147.  
  148. foreach ($childpages as $page_id => $page)
  149. {
  150. //Clear the vars in case we have multiple shortcodes on a single post
  151. $feat_image = NULL;
  152. // $attachmenturl = NULL;
  153. // $the_title_html = NULL;
  154.  
  155. $thispage = $page->ID; //Store the child page ID
  156.  
  157. //Start building the link to the childpage
  158. $the_pagelink = get_permalink($thispage);
  159. $output .= "<a href='" . $the_pagelink . "' class='gallery_children" . $id . " gallery_children " . $style . "' style='text-align:" . $text_align . ";height:" . $height . "px;display:inline-block;" . $width;
  160.  
  161. //Store the page title formatted in HTML in case we need it
  162. $the_page_title = get_the_title($thispage);
  163. $the_title_html = "<" . $selector . " style='float:left'>" . $the_page_title . "</" . $selector . ">";
  164.  
  165. if($showimages)
  166. {
  167. $feat_image = wp_get_attachment_image_src( get_post_thumbnail_id($thispage), $size );
  168.  
  169. // Use the Featured for a background image if we have one specified
  170. if($feat_image)
  171. {
  172. $feat_url = $feat_image[0];
  173. $output .= ";background-image: url(\"" . $feat_url . "\");background-position:center;' alt='{$the_page_title}' title='{$the_page_title}'>";
  174.  
  175. if($pagetitle)
  176. {
  177. $output .= $the_title_html;
  178. }
  179.  
  180. } else //Look for attachments to the page and use one of those
  181. {
  182. $childattachments = & get_children(array('post_parent' => $thispage, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'orderby' => 'menu_order', 'order' => 'DESC'));
  183.  
  184. if (!empty($childattachments))
  185. {
  186. $first_attachment = array_shift($childattachments);
  187. $attachmenturl = wp_get_attachment_image_src($first_attachment->ID, $size);
  188.  
  189. if(!empty($attachmenturl[0]))
  190. {
  191. $output .= ";background-image: url(\"" . $attachmenturl[0] . "\");background-position:center;' >";
  192.  
  193. //Display a page title if they set the option to true, i.e $pagetitle="1"
  194. if($pagetitle)
  195. {
  196. $output .= $the_title_html;
  197. }
  198.  
  199. } else //If the attachments are not images, we will just use the title
  200. {
  201. $output .= "'>" . $the_title_html;
  202. }
  203. } else // Must be image attachments, use the title instead
  204. {
  205. $output .= "'>" . $the_title_html;
  206. } //End check for images attached to the post
  207.  
  208. } // End if checking for Featured or other images
  209.  
  210. } else //Not showing images at all, we'll use the title
  211. {
  212. $output .= "'>" .$the_title_html;
  213. }//End if Show Images
  214.  
  215. $output .= "</a>";
  216. } //End For Each
  217. $output .= "</div>" . $pagination;
  218.  
  219. } //End check for output
  220. return $output;
  221. } //End function
  222.  
  223. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement