Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: Bryans Childpage Thumbnail Generator
- Plugin URI: http://www.bryanleister.com/projects/childpages-plugin/
- 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.
- Version: 1.2
- Author: Bryan Leister
- Author URI: http://bryanleister.com
- License: GPL2
- */
- function endsWith($haystack, $needle)
- {
- return $needle === "" || substr($haystack, -strlen($needle)) === $needle;
- }
- function current_page_url() {
- $pageURL = 'http';
- if( isset($_SERVER["HTTPS"]) ) {
- if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
- }
- $pageURL .= "://";
- if ($_SERVER["SERVER_PORT"] != "80") {
- $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
- } else {
- $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
- }
- return $pageURL;
- }
- add_shortcode('gallery_childpages', 'gallery_childpages_shortcode');
- /* Child Pages code
- Based on the Gallery code we are finding all of the Children of a Page and then displaying
- them as either a thumbnail or as an image thumbnail.
- */
- function gallery_childpages_shortcode($attr)
- {
- global $post, $wp_locale;
- static $instance = 0;
- $instance++;
- $childpages = array();
- // Allow plugins/themes to override the default gallery template.
- $output = apply_filters('post_gallery', '', $attr);
- if ( $output != '' )
- {
- return $output;
- }
- // We're trusting author input, so let's at least make sure it looks like a valid orderby statement
- if ( isset( $attr['orderby'] ) )
- {
- $attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
- if ( !$attr['orderby'] )
- {
- unset( $attr['orderby'] );
- }
- }
- extract(shortcode_atts(array(
- 'order' => 'ASC',
- 'orderby' => 'title',
- 'id' => '',
- 'number' => -1,
- 'height' => '100',
- 'width' => '',
- 'size' => 'thumbnail',
- 'style' => '',
- 'include' => '',
- 'pagetitle' => false,
- 'selector' => 'h2',
- 'selector_height' => '30px',
- 'selector_width' => '100%',
- 'selector_padding' => '',
- 'showimages' => true,
- 'text_align' => 'left',
- 'text_margin' => '',
- 'exclude' => '',
- 'posts_per_page' => '5',
- 'page_number' => '1'
- ), $attr));
- if( $id == '')
- {
- $id = $post->ID;
- }
- $id = intval($id);
- if($width != null)
- {
- $width='width:' . $width . 'px';
- }
- // $width="width:140px";
- if ($order == 'RAND')
- {
- $orderby = 'none';
- }
- if($text_margin == '')
- {
- $textmargin = ($height/2)-10;
- $textmargin .= "px 0 0 0";
- } else
- {
- $textmargin = $text_margin;
- }
- // $textmargin = ($height)-30;
- $offsettext = ($height*3);
- $offset = ($page_number - 1) * $posts_per_page;
- $countposts = count(get_posts('post_parent=' . $id . '&post_type=page&post_status=publish&numberposts=' . $number . '&exclude=' . $exclude . '&orderby=' . $orderby));
- $totalpages = $countposts / $posts_per_page;
- $totalpages = intval($totalpages) + ($totalpages > intval($totalpages) ? 1 : 0);
- $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);
- //
- //Check if we have anything to output
- if ( empty($childpages))
- {
- $output = "<p>There are no child pages for post id # " . $id . "</p>";
- } else //We do have childpages, let's loop through them
- {
- $current_url = current_page_url();
- $baseurl = endsWith($current_url, $post->post_name) ? $current_url . "/" : "";
- $pagination = "<div class='pagination'>";
- for($i=1; $i <= $totalpages; ++$i)
- if($i == 1 && $page_number == 0)
- $pagination .= "<a class='page_number active' href='" . $baseurl . "1/'>1</a>";
- else
- $pagination .= "<a class='page_number" . ($page_number == $i ? " active" : "") . "' href='" . $baseurl . $i . "/'>" . $i . "</a>";
- $pagination .= "</div>";
- //Nest the gallery inside a DIV to prevent Wordpress from adding a <p> to the post and screwing it up...
- $output = "<div class='clear'>";
- foreach ($childpages as $page_id => $page)
- {
- //Clear the vars in case we have multiple shortcodes on a single post
- $feat_image = NULL;
- // $attachmenturl = NULL;
- // $the_title_html = NULL;
- $thispage = $page->ID; //Store the child page ID
- //Start building the link to the childpage
- $the_pagelink = get_permalink($thispage);
- $output .= "<a href='" . $the_pagelink . "' class='gallery_children" . $id . " gallery_children " . $style . "' style='text-align:" . $text_align . ";height:" . $height . "px;display:inline-block;" . $width;
- //Store the page title formatted in HTML in case we need it
- $the_page_title = get_the_title($thispage);
- $the_title_html = "<" . $selector . " style='float:left'>" . $the_page_title . "</" . $selector . ">";
- if($showimages)
- {
- $feat_image = wp_get_attachment_image_src( get_post_thumbnail_id($thispage), $size );
- // Use the Featured for a background image if we have one specified
- if($feat_image)
- {
- $feat_url = $feat_image[0];
- $output .= ";background-image: url(\"" . $feat_url . "\");background-position:center;' alt='{$the_page_title}' title='{$the_page_title}'>";
- if($pagetitle)
- {
- $output .= $the_title_html;
- }
- } else //Look for attachments to the page and use one of those
- {
- $childattachments = & get_children(array('post_parent' => $thispage, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'orderby' => 'menu_order', 'order' => 'DESC'));
- if (!empty($childattachments))
- {
- $first_attachment = array_shift($childattachments);
- $attachmenturl = wp_get_attachment_image_src($first_attachment->ID, $size);
- if(!empty($attachmenturl[0]))
- {
- $output .= ";background-image: url(\"" . $attachmenturl[0] . "\");background-position:center;' >";
- //Display a page title if they set the option to true, i.e $pagetitle="1"
- if($pagetitle)
- {
- $output .= $the_title_html;
- }
- } else //If the attachments are not images, we will just use the title
- {
- $output .= "'>" . $the_title_html;
- }
- } else // Must be image attachments, use the title instead
- {
- $output .= "'>" . $the_title_html;
- } //End check for images attached to the post
- } // End if checking for Featured or other images
- } else //Not showing images at all, we'll use the title
- {
- $output .= "'>" .$the_title_html;
- }//End if Show Images
- $output .= "</a>";
- } //End For Each
- $output .= "</div>" . $pagination;
- } //End check for output
- return $output;
- } //End function
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement