4 // "The Loop" post functions
13 function get_the_ID() {
19 function the_title($before = '', $after = '', $echo = true) {
20 $title = get_the_title();
22 if ( strlen($title) == 0 )
25 $title = $before . $title . $after;
33 function the_title_attribute( $args = '' ) {
34 $title = get_the_title();
36 if ( strlen($title) == 0 )
39 $defaults = array('before' => '', 'after' => '', 'echo' => true);
40 $r = wp_parse_args($args, $defaults);
41 extract( $r, EXTR_SKIP );
44 $title = $before . $title . $after;
45 $title = attribute_escape(strip_tags($title));
53 function get_the_title( $id = 0 ) {
54 $post = &get_post($id);
56 $title = $post->post_title;
57 if ( !empty($post->post_password) )
58 $title = sprintf(__('Protected: %s'), $title);
59 else if ( 'private' == $post->post_status )
60 $title = sprintf(__('Private: %s'), $title);
62 return apply_filters( 'the_title', $title );
65 function the_guid( $id = 0 ) {
66 echo get_the_guid($id);
69 function get_the_guid( $id = 0 ) {
70 $post = &get_post($id);
72 return apply_filters('get_the_guid', $post->guid);
75 function the_content($more_link_text = '(more...)', $stripteaser = 0, $more_file = '') {
76 $content = get_the_content($more_link_text, $stripteaser, $more_file);
77 $content = apply_filters('the_content', $content);
78 $content = str_replace(']]>', ']]>', $content);
83 function get_the_content($more_link_text = '(more...)', $stripteaser = 0, $more_file = '') {
84 global $id, $post, $more, $single, $withcomments, $page, $pages, $multipage, $numpages;
89 if ( !empty($post->post_password) ) { // if there's a password
90 if ( stripslashes($_COOKIE['wp-postpass_'.COOKIEHASH]) != $post->post_password ) { // and it doesn't match the cookie
91 $output = get_the_password_form();
96 if ( $more_file != '' )
99 $file = $pagenow; //$_SERVER['PHP_SELF'];
101 if ( $page > count($pages) ) // if the requested page doesn't exist
102 $page = count($pages); // give them the highest numbered page that DOES exist
104 $content = $pages[$page-1];
105 if ( preg_match('/<!--more(.*?)?-->/', $content, $matches) ) {
106 $content = explode($matches[0], $content, 2);
107 if ( !empty($matches[1]) && !empty($more_link_text) )
108 $more_link_text = strip_tags(wp_kses_no_null(trim($matches[1])));
110 $content = array($content);
112 if ( (false !== strpos($post->post_content, '<!--noteaser-->') && ((!$multipage) || ($page==1))) )
114 $teaser = $content[0];
115 if ( ($more) && ($stripteaser) )
118 if ( count($content) > 1 ) {
120 $output .= '<span id="more-'.$id.'"></span>'.$content[1];
122 $output = balanceTags($output);
123 if ( ! empty($more_link_text) )
124 $output .= ' <a href="'. get_permalink() . "#more-$id\" class=\"more-link\">$more_link_text</a>";
128 if ( $preview ) // preview fix for javascript bug with foreign languages
129 $output = preg_replace('/\%u([0-9A-F]{4,4})/e', "'&#'.base_convert('\\1',16,10).';'", $output);
135 function the_excerpt() {
136 echo apply_filters('the_excerpt', get_the_excerpt());
140 function get_the_excerpt($deprecated = true) {
143 $output = $post->post_excerpt;
144 if ( !empty($post->post_password) ) { // if there's a password
145 if ( $_COOKIE['wp-postpass_'.COOKIEHASH] != $post->post_password ) { // and it doesn't match the cookie
146 $output = __('There is no excerpt because this is a protected post.');
151 return apply_filters('get_the_excerpt', $output);
154 function has_excerpt( $id = 0 ) {
155 $post = &get_post( $id );
156 return ( !empty( $post->post_excerpt ) );
159 function wp_link_pages($args = '') {
161 'before' => '<p>' . __('Pages:'), 'after' => '</p>',
162 'next_or_number' => 'number', 'nextpagelink' => __('Next page'),
163 'previouspagelink' => __('Previous page'), 'pagelink' => '%',
164 'more_file' => '', 'echo' => 1
167 $r = wp_parse_args( $args, $defaults );
168 extract( $r, EXTR_SKIP );
170 global $post, $id, $page, $numpages, $multipage, $more, $pagenow;
171 if ( $more_file != '' )
178 if ( 'number' == $next_or_number ) {
180 for ( $i = 1; $i < ($numpages+1); $i = $i + 1 ) {
181 $j = str_replace('%',"$i",$pagelink);
183 if ( ($i != $page) || ((!$more) && ($page==1)) ) {
185 $output .= '<a href="' . get_permalink() . '">';
187 if ( '' == get_option('permalink_structure') || in_array($post->post_status, array('draft', 'pending')) )
188 $output .= '<a href="' . get_permalink() . '&page=' . $i . '">';
190 $output .= '<a href="' . trailingslashit(get_permalink()) . user_trailingslashit($i, 'single_paged') . '">';
194 if ( ($i != $page) || ((!$more) && ($page==1)) )
204 $output .= '<a href="' . get_permalink() . '">' . $previouspagelink . '</a>';
206 if ( '' == get_option('permalink_structure') || in_array($post->post_status, array('draft', 'pending')) )
207 $output .= '<a href="' . get_permalink() . '&page=' . $i . '">' . $previouspagelink . '</a>';
209 $output .= '<a href="' . trailingslashit(get_permalink()) . user_trailingslashit($i, 'single_paged') . '">' . $previouspagelink . '</a>';
213 if ( $i <= $numpages && $more ) {
215 $output .= '<a href="' . get_permalink() . '">' . $nextpagelink . '</a>';
217 if ( '' == get_option('permalink_structure') || in_array($post->post_status, array('draft', 'pending')) )
218 $output .= '<a href="' . get_permalink() . '&page=' . $i . '">' . $nextpagelink . '</a>';
220 $output .= '<a href="' . trailingslashit(get_permalink()) . user_trailingslashit($i, 'single_paged') . '">' . $nextpagelink . '</a>';
236 // Post-meta: Custom per-post fields.
240 function post_custom( $key = '' ) {
241 $custom = get_post_custom();
243 if ( 1 == count($custom[$key]) )
244 return $custom[$key][0];
246 return $custom[$key];
250 // this will probably change at some point...
251 function the_meta() {
254 if ( $keys = get_post_custom_keys() ) {
255 echo "<ul class='post-meta'>\n";
256 foreach ( $keys as $key ) {
258 if ( '_' == $keyt{0} )
260 $values = array_map('trim', get_post_custom_values($key));
261 $value = implode($values,', ');
262 echo apply_filters('the_meta_key', "<li><span class='post-meta-key'>$key:</span> $value</li>\n", $key, $value);
273 function wp_dropdown_pages($args = '') {
275 'depth' => 0, 'child_of' => 0,
276 'selected' => 0, 'echo' => 1,
277 'name' => 'page_id', 'show_option_none' => ''
280 $r = wp_parse_args( $args, $defaults );
281 extract( $r, EXTR_SKIP );
283 $pages = get_pages($r);
286 if ( ! empty($pages) ) {
287 $output = "<select name='$name'>\n";
288 if ( $show_option_none )
289 $output .= "\t<option value=''>$show_option_none</option>\n";
290 $output .= walk_page_dropdown_tree($pages, $depth, $r);
291 $output .= "</select>\n";
294 $output = apply_filters('wp_dropdown_pages', $output);
302 function wp_list_pages($args = '') {
304 'depth' => 0, 'show_date' => '',
305 'date_format' => get_option('date_format'),
306 'child_of' => 0, 'exclude' => '',
307 'title_li' => __('Pages'), 'echo' => 1,
308 'authors' => '', 'sort_column' => 'menu_order, post_title'
311 $r = wp_parse_args( $args, $defaults );
312 extract( $r, EXTR_SKIP );
317 // sanitize, mostly to keep spaces out
318 $r['exclude'] = preg_replace('[^0-9,]', '', $r['exclude']);
320 // Allow plugins to filter an array of excluded pages
321 $r['exclude'] = implode(',', apply_filters('wp_list_pages_excludes', explode(',', $r['exclude'])));
324 $pages = get_pages($r);
326 if ( !empty($pages) ) {
327 if ( $r['title_li'] )
328 $output .= '<li class="pagenav">' . $r['title_li'] . '<ul>';
332 $current_page = $wp_query->get_queried_object_id();
333 $output .= walk_page_tree($pages, $r['depth'], $current_page, $r);
335 if ( $r['title_li'] )
336 $output .= '</ul></li>';
339 $output = apply_filters('wp_list_pages', $output);
351 function walk_page_tree() {
352 $walker = new Walker_Page;
353 $args = func_get_args();
354 return call_user_func_array(array(&$walker, 'walk'), $args);
357 function walk_page_dropdown_tree() {
358 $walker = new Walker_PageDropdown;
359 $args = func_get_args();
360 return call_user_func_array(array(&$walker, 'walk'), $args);
367 function the_attachment_link($id = 0, $fullsize = false, $max_dims = false) {
368 echo get_the_attachment_link($id, $fullsize, $max_dims);
371 function get_the_attachment_link($id = 0, $fullsize = false, $max_dims = false) {
373 $_post = & get_post($id);
375 if ( ('attachment' != $_post->post_type) || !$url = wp_get_attachment_url($_post->ID) )
376 return __('Missing Attachment');
378 $post_title = attribute_escape($_post->post_title);
380 $innerHTML = get_attachment_innerHTML($_post->ID, $fullsize, $max_dims);
381 return "<a href='$url' title='$post_title'>$innerHTML</a>";
384 function get_attachment_icon_src( $id = 0, $fullsize = false ) {
386 if ( !$post = & get_post($id) )
389 $imagedata = wp_get_attachment_metadata( $post->ID );
391 $file = get_attached_file( $post->ID );
393 if ( !$fullsize && $thumbfile = wp_get_attachment_thumb_file( $post->ID ) ) {
394 // We have a thumbnail desired, specified and existing
396 $src = wp_get_attachment_thumb_url( $post->ID );
397 $src_file = $thumbfile;
398 $class = 'attachmentthumb';
399 } elseif ( wp_attachment_is_image( $post->ID ) ) {
400 // We have an image without a thumbnail
402 $src = wp_get_attachment_url( $post->ID );
404 $class = 'attachmentimage';
405 } elseif ( $src = wp_mime_type_icon( $post->ID ) ) {
406 // No thumb, no image. We'll look for a mime-related icon instead.
408 $icon_dir = apply_filters( 'icon_dir', get_template_directory() . '/images' );
409 $src_file = $icon_dir . '/' . basename($src);
412 if ( !isset($src) || !$src )
415 return array($src, $src_file);
418 function get_attachment_icon( $id = 0, $fullsize = false, $max_dims = false ) {
420 if ( !$post = & get_post($id) )
423 if ( !$src = get_attachment_icon_src( $post->ID, $fullsize ) )
426 list($src, $src_file) = $src;
428 // Do we need to constrain the image?
429 if ( ($max_dims = apply_filters('attachment_max_dims', $max_dims)) && file_exists($src_file) ) {
431 $imagesize = getimagesize($src_file);
433 if (($imagesize[0] > $max_dims[0]) || $imagesize[1] > $max_dims[1] ) {
434 $actual_aspect = $imagesize[0] / $imagesize[1];
435 $desired_aspect = $max_dims[0] / $max_dims[1];
437 if ( $actual_aspect >= $desired_aspect ) {
438 $height = $actual_aspect * $max_dims[0];
439 $constraint = "width='{$max_dims[0]}' ";
440 $post->iconsize = array($max_dims[0], $height);
442 $width = $max_dims[1] / $actual_aspect;
443 $constraint = "height='{$max_dims[1]}' ";
444 $post->iconsize = array($width, $max_dims[1]);
447 $post->iconsize = array($imagesize[0], $imagesize[1]);
451 $post_title = attribute_escape($post->post_title);
453 $icon = "<img src='$src' title='$post_title' alt='$post_title' $constraint/>";
455 return apply_filters( 'attachment_icon', $icon, $post->ID );
458 function get_attachment_innerHTML($id = 0, $fullsize = false, $max_dims = false) {
460 if ( !$post = & get_post($id) )
463 if ( $innerHTML = get_attachment_icon($post->ID, $fullsize, $max_dims))
467 $innerHTML = attribute_escape($post->post_title);
469 return apply_filters('attachment_innerHTML', $innerHTML, $post->ID);
472 function prepend_attachment($content) {
473 $p = '<p class="attachment">';
474 $p .= get_the_attachment_link(false, true, array(400, 300));
476 $p = apply_filters('prepend_attachment', $p);
478 return "$p\n$content";
485 function get_the_password_form() {
486 $output = '<form action="' . get_option('siteurl') . '/wp-pass.php" method="post">
487 <p>' . __("This post is password protected. To view it please enter your password below:") . '</p>
488 <p><label>' . __("Password:") . ' <input name="post_password" type="password" size="20" /></label> <input type="submit" name="Submit" value="' . __("Submit") . '" /></p>