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;
59 if ( !empty($post->post_password) )
60 $title = sprintf(__('Protected: %s'), $title);
61 else if ( isset($post->post_status) && 'private' == $post->post_status )
62 $title = sprintf(__('Private: %s'), $title);
64 return apply_filters( 'the_title', $title );
67 function the_guid( $id = 0 ) {
68 echo get_the_guid($id);
71 function get_the_guid( $id = 0 ) {
72 $post = &get_post($id);
74 return apply_filters('get_the_guid', $post->guid);
77 function the_content($more_link_text = '(more...)', $stripteaser = 0, $more_file = '') {
78 $content = get_the_content($more_link_text, $stripteaser, $more_file);
79 $content = apply_filters('the_content', $content);
80 $content = str_replace(']]>', ']]>', $content);
85 function get_the_content($more_link_text = '(more...)', $stripteaser = 0, $more_file = '') {
86 global $id, $post, $more, $page, $pages, $multipage, $preview, $pagenow;
90 if ( !empty($post->post_password) ) { // if there's a password
91 if ( !isset($_COOKIE['wp-postpass_'.COOKIEHASH]) || stripslashes($_COOKIE['wp-postpass_'.COOKIEHASH]) != $post->post_password ) { // and it doesn't match the cookie
92 $output = get_the_password_form();
97 if ( $more_file != '' )
100 $file = $pagenow; //$_SERVER['PHP_SELF'];
102 if ( $page > count($pages) ) // if the requested page doesn't exist
103 $page = count($pages); // give them the highest numbered page that DOES exist
105 $content = $pages[$page-1];
106 if ( preg_match('/<!--more(.*?)?-->/', $content, $matches) ) {
107 $content = explode($matches[0], $content, 2);
108 if ( !empty($matches[1]) && !empty($more_link_text) )
109 $more_link_text = strip_tags(wp_kses_no_null(trim($matches[1])));
111 $content = array($content);
113 if ( (false !== strpos($post->post_content, '<!--noteaser-->') && ((!$multipage) || ($page==1))) )
115 $teaser = $content[0];
116 if ( ($more) && ($stripteaser) )
119 if ( count($content) > 1 ) {
121 $output .= '<span id="more-'.$id.'"></span>'.$content[1];
123 $output = balanceTags($output);
124 if ( ! empty($more_link_text) )
125 $output .= ' <a href="'. get_permalink() . "#more-$id\" class=\"more-link\">$more_link_text</a>";
129 if ( $preview ) // preview fix for javascript bug with foreign languages
130 $output = preg_replace('/\%u([0-9A-F]{4,4})/e', "'&#'.base_convert('\\1',16,10).';'", $output);
136 function the_excerpt() {
137 echo apply_filters('the_excerpt', get_the_excerpt());
141 function get_the_excerpt($deprecated = '') {
144 $output = $post->post_excerpt;
145 if ( !empty($post->post_password) ) { // if there's a password
146 if ( !isset($_COOKIE['wp-postpass_'.COOKIEHASH]) || $_COOKIE['wp-postpass_'.COOKIEHASH] != $post->post_password ) { // and it doesn't match the cookie
147 $output = __('There is no excerpt because this is a protected post.');
152 return apply_filters('get_the_excerpt', $output);
155 function has_excerpt( $id = 0 ) {
156 $post = &get_post( $id );
157 return ( !empty( $post->post_excerpt ) );
160 function wp_link_pages($args = '') {
162 'before' => '<p>' . __('Pages:'), 'after' => '</p>',
163 'next_or_number' => 'number', 'nextpagelink' => __('Next page'),
164 'previouspagelink' => __('Previous page'), 'pagelink' => '%',
165 'more_file' => '', 'echo' => 1
168 $r = wp_parse_args( $args, $defaults );
169 extract( $r, EXTR_SKIP );
171 global $post, $page, $numpages, $multipage, $more, $pagenow;
172 if ( $more_file != '' )
179 if ( 'number' == $next_or_number ) {
181 for ( $i = 1; $i < ($numpages+1); $i = $i + 1 ) {
182 $j = str_replace('%',"$i",$pagelink);
184 if ( ($i != $page) || ((!$more) && ($page==1)) ) {
186 $output .= '<a href="' . get_permalink() . '">';
188 if ( '' == get_option('permalink_structure') || in_array($post->post_status, array('draft', 'pending')) )
189 $output .= '<a href="' . get_permalink() . '&page=' . $i . '">';
191 $output .= '<a href="' . trailingslashit(get_permalink()) . user_trailingslashit($i, 'single_paged') . '">';
195 if ( ($i != $page) || ((!$more) && ($page==1)) )
205 $output .= '<a href="' . get_permalink() . '">' . $previouspagelink . '</a>';
207 if ( '' == get_option('permalink_structure') || in_array($post->post_status, array('draft', 'pending')) )
208 $output .= '<a href="' . get_permalink() . '&page=' . $i . '">' . $previouspagelink . '</a>';
210 $output .= '<a href="' . trailingslashit(get_permalink()) . user_trailingslashit($i, 'single_paged') . '">' . $previouspagelink . '</a>';
214 if ( $i <= $numpages && $more ) {
216 $output .= '<a href="' . get_permalink() . '">' . $nextpagelink . '</a>';
218 if ( '' == get_option('permalink_structure') || in_array($post->post_status, array('draft', 'pending')) )
219 $output .= '<a href="' . get_permalink() . '&page=' . $i . '">' . $nextpagelink . '</a>';
221 $output .= '<a href="' . trailingslashit(get_permalink()) . user_trailingslashit($i, 'single_paged') . '">' . $nextpagelink . '</a>';
237 // Post-meta: Custom per-post fields.
241 function post_custom( $key = '' ) {
242 $custom = get_post_custom();
244 if ( 1 == count($custom[$key]) )
245 return $custom[$key][0];
247 return $custom[$key];
251 // this will probably change at some point...
252 function the_meta() {
253 if ( $keys = get_post_custom_keys() ) {
254 echo "<ul class='post-meta'>\n";
255 foreach ( $keys as $key ) {
257 if ( '_' == $keyt{0} )
259 $values = array_map('trim', get_post_custom_values($key));
260 $value = implode($values,', ');
261 echo apply_filters('the_meta_key', "<li><span class='post-meta-key'>$key:</span> $value</li>\n", $key, $value);
272 function wp_dropdown_pages($args = '') {
274 'depth' => 0, 'child_of' => 0,
275 'selected' => 0, 'echo' => 1,
276 'name' => 'page_id', 'show_option_none' => ''
279 $r = wp_parse_args( $args, $defaults );
280 extract( $r, EXTR_SKIP );
282 $pages = get_pages($r);
285 if ( ! empty($pages) ) {
286 $output = "<select name='$name' id='$name'>\n";
287 if ( $show_option_none )
288 $output .= "\t<option value=''>$show_option_none</option>\n";
289 $output .= walk_page_dropdown_tree($pages, $depth, $r);
290 $output .= "</select>\n";
293 $output = apply_filters('wp_dropdown_pages', $output);
301 function wp_list_pages($args = '') {
303 'depth' => 0, 'show_date' => '',
304 'date_format' => get_option('date_format'),
305 'child_of' => 0, 'exclude' => '',
306 'title_li' => __('Pages'), 'echo' => 1,
307 'authors' => '', 'sort_column' => 'menu_order, post_title'
310 $r = wp_parse_args( $args, $defaults );
311 extract( $r, EXTR_SKIP );
316 // sanitize, mostly to keep spaces out
317 $r['exclude'] = preg_replace('[^0-9,]', '', $r['exclude']);
319 // Allow plugins to filter an array of excluded pages
320 $r['exclude'] = implode(',', apply_filters('wp_list_pages_excludes', explode(',', $r['exclude'])));
323 $r['hierarchical'] = 0;
324 $pages = get_pages($r);
326 if ( !empty($pages) ) {
327 if ( $r['title_li'] )
328 $output .= '<li class="pagenav">' . $r['title_li'] . '<ul>';
331 if ( is_page() || $wp_query->is_posts_page )
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, $deprecated = false, $permalink = false) {
369 echo wp_get_attachment_link($id, 'full', $permalink);
371 echo wp_get_attachment_link($id, 'thumbnail', $permalink);
374 // get an attachment page link using an image or icon if possible
375 function wp_get_attachment_link($id = 0, $size = 'thumbnail', $permalink = false, $icon = false) {
377 $_post = & get_post( $id );
379 if ( ('attachment' != $_post->post_type) || !$url = wp_get_attachment_url($_post->ID) )
380 return __('Missing Attachment');
383 $url = get_attachment_link($_post->ID);
385 $post_title = attribute_escape($_post->post_title);
387 $link_text = wp_get_attachment_image($id, $size, $icon);
389 $link_text = $_post->post_title;
391 return "<a href='$url' title='$post_title'>$link_text</a>";
395 // deprecated - use wp_get_attachment_link()
396 function get_the_attachment_link($id = 0, $fullsize = false, $max_dims = false, $permalink = false) {
398 $_post = & get_post($id);
400 if ( ('attachment' != $_post->post_type) || !$url = wp_get_attachment_url($_post->ID) )
401 return __('Missing Attachment');
404 $url = get_attachment_link($_post->ID);
406 $post_title = attribute_escape($_post->post_title);
408 $innerHTML = get_attachment_innerHTML($_post->ID, $fullsize, $max_dims);
409 return "<a href='$url' title='$post_title'>$innerHTML</a>";
413 // deprecated: use wp_get_attachment_image_src()
414 function get_attachment_icon_src( $id = 0, $fullsize = false ) {
416 if ( !$post = & get_post($id) )
419 $file = get_attached_file( $post->ID );
421 if ( !$fullsize && $src = wp_get_attachment_thumb_url( $post->ID ) ) {
422 // We have a thumbnail desired, specified and existing
424 $src_file = basename($src);
425 $class = 'attachmentthumb';
426 } elseif ( wp_attachment_is_image( $post->ID ) ) {
427 // We have an image without a thumbnail
429 $src = wp_get_attachment_url( $post->ID );
431 $class = 'attachmentimage';
432 } elseif ( $src = wp_mime_type_icon( $post->ID ) ) {
433 // No thumb, no image. We'll look for a mime-related icon instead.
435 $icon_dir = apply_filters( 'icon_dir', get_template_directory() . '/images' );
436 $src_file = $icon_dir . '/' . basename($src);
439 if ( !isset($src) || !$src )
442 return array($src, $src_file);
445 // deprecated: use wp_get_attachment_image()
446 function get_attachment_icon( $id = 0, $fullsize = false, $max_dims = false ) {
448 if ( !$post = & get_post($id) )
451 if ( !$src = get_attachment_icon_src( $post->ID, $fullsize ) )
454 list($src, $src_file) = $src;
456 // Do we need to constrain the image?
457 if ( ($max_dims = apply_filters('attachment_max_dims', $max_dims)) && file_exists($src_file) ) {
459 $imagesize = getimagesize($src_file);
461 if (($imagesize[0] > $max_dims[0]) || $imagesize[1] > $max_dims[1] ) {
462 $actual_aspect = $imagesize[0] / $imagesize[1];
463 $desired_aspect = $max_dims[0] / $max_dims[1];
465 if ( $actual_aspect >= $desired_aspect ) {
466 $height = $actual_aspect * $max_dims[0];
467 $constraint = "width='{$max_dims[0]}' ";
468 $post->iconsize = array($max_dims[0], $height);
470 $width = $max_dims[1] / $actual_aspect;
471 $constraint = "height='{$max_dims[1]}' ";
472 $post->iconsize = array($width, $max_dims[1]);
475 $post->iconsize = array($imagesize[0], $imagesize[1]);
482 $post_title = attribute_escape($post->post_title);
484 $icon = "<img src='$src' title='$post_title' alt='$post_title' $constraint/>";
486 return apply_filters( 'attachment_icon', $icon, $post->ID );
489 // deprecated: use wp_get_attachment_image()
490 function get_attachment_innerHTML($id = 0, $fullsize = false, $max_dims = false) {
492 if ( !$post = & get_post($id) )
495 if ( $innerHTML = get_attachment_icon($post->ID, $fullsize, $max_dims))
499 $innerHTML = attribute_escape($post->post_title);
501 return apply_filters('attachment_innerHTML', $innerHTML, $post->ID);
504 function prepend_attachment($content) {
507 if ( empty($post->post_type) || $post->post_type != 'attachment' )
510 $p = '<p class="attachment">';
511 // show the medium sized image representation of the attachment if available, and link to the raw file
512 $p .= wp_get_attachment_link(0, 'medium', false);
514 $p = apply_filters('prepend_attachment', $p);
516 return "$p\n$content";
523 function get_the_password_form() {
525 $label = 'pwbox-'.(empty($post->ID) ? rand() : $post->ID);
526 $output = '<form action="' . get_option('siteurl') . '/wp-pass.php" method="post">
527 <p>' . __("This post is password protected. To view it please enter your password below:") . '</p>
528 <p><label for="' . $label . '">' . __("Password:") . ' <input name="post_password" id="' . $label . '" type="password" size="20" /></label> <input type="submit" name="Submit" value="' . __("Submit") . '" /></p>
535 * is_page_template() - Determine wether or not we are in a page template
537 * This template tag allows you to determine wether or not you are in a page template.
538 * You can optional provide a template name and then the check will be specific to
541 * @package Template Tags
542 * @global object $wp_query
543 * @param string $template The specific template name if specific matching is required
545 function is_page_template($template = '') {
552 $page = $wp_query->get_queried_object();
553 $custom_fields = get_post_custom_values('_wp_page_template',$page->ID);
554 $page_template = $custom_fields[0];
556 // We have no argument passed so just see if a page_template has been specified
557 if ( empty( $template ) ) {
558 if (!empty( $page_template ) ) {
561 } elseif ( $template == $page_template) {
569 * wp_post_revision_title() - returns formatted datetimestamp of a revision (linked to that revisions's page)
572 * @subpackage Post Revisions
577 * @param int|object $revision revision ID or revision object
578 * @param bool $link optional Link to revisions's page?
579 * @return string i18n formatted datetimestamp or localized 'Corrent Revision'
581 function wp_post_revision_title( $revision, $link = true ) {
582 if ( !$revision = get_post( $revision ) )
585 if ( !in_array( $revision->post_type, array( 'post', 'page', 'revision' ) ) )
588 $datef = _c( 'j F, Y @ G:i|revision date format');
589 $autosavef = __( '%s [Autosave]' );
590 $currentf = __( '%s [Current Revision]' );
592 $date = date_i18n( $datef, strtotime( $revision->post_modified_gmt . ' +0000' ) );
593 if ( $link && current_user_can( 'edit_post', $revision->ID ) && $link = get_edit_post_link( $revision->ID ) )
594 $date = "<a href='$link'>$date</a>";
596 if ( !wp_is_post_revision( $revision ) )
597 $date = sprintf( $currentf, $date );
598 elseif ( wp_is_post_autosave( $revision ) )
599 $date = sprintf( $autosavef, $date );
605 * wp_list_post_revisions() - echoes list of a post's revisions
607 * Can output either a UL with edit links or a TABLE with diff interface, and restore action links
609 * Second argument controls parameters:
610 * (bool) parent : include the parent (the "Current Revision") in the list
611 * (string) format : 'list' or 'form-table'. 'list' outputs UL, 'form-table' outputs TABLE with UI
612 * (int) right : what revision is currently being viewed - used in form-table format
613 * (int) left : what revision is currently being diffed against right - used in form-table format
616 * @subpackage Post Revisions
619 * @uses wp_get_post_revisions()
620 * @uses wp_post_revision_title()
621 * @uses get_edit_post_link()
622 * @uses get_author_name()
624 * @param int|object $post_id post ID or post object
625 * @param string|array $args see description @see wp_parse_args()
627 function wp_list_post_revisions( $post_id = 0, $args = null ) { // TODO? split into two functions (list, form-table) ?
628 if ( !$post = get_post( $post_id ) )
631 $defaults = array( 'parent' => false, 'right' => false, 'left' => false, 'format' => 'list', 'type' => 'all' );
632 extract( wp_parse_args( $args, $defaults ), EXTR_SKIP );
636 if ( !$autosave = wp_get_post_autosave( $post->ID ) )
638 $revisions = array( $autosave );
640 case 'revision' : // just revisions - remove autosave later
643 if ( !$revisions = wp_get_post_revisions( $post->ID ) )
648 $titlef = _c( '%1$s by %2$s|post revision 1:datetime, 2:name' );
651 array_unshift( $revisions, $post );
655 $can_edit_post = current_user_can( 'edit_post', $post->ID );
656 foreach ( $revisions as $revision ) {
657 if ( !current_user_can( 'read_post', $revision->ID ) )
659 if ( 'revision' === $type && wp_is_post_autosave( $revision ) )
662 $date = wp_post_revision_title( $revision );
663 $name = get_author_name( $revision->post_author );
665 if ( 'form-table' == $format ) {
667 $left_checked = $left == $revision->ID ? ' checked="checked"' : '';
669 $left_checked = $right_checked ? ' checked="checked"' : ''; // [sic] (the next one)
670 $right_checked = $right == $revision->ID ? ' checked="checked"' : '';
672 $class = $class ? '' : " class='alternate'";
674 if ( $post->ID != $revision->ID && $can_edit_post )
675 $actions = '<a href="' . wp_nonce_url( add_query_arg( array( 'revision' => $revision->ID, 'diff' => false, 'action' => 'restore' ) ), "restore-post_$post->ID|$revision->ID" ) . '">' . __( 'Restore' ) . '</a>';
679 $rows .= "<tr$class>\n";
680 $rows .= "\t<th style='white-space: nowrap' scope='row'><input type='radio' name='left' value='$revision->ID'$left_checked /><input type='radio' name='right' value='$revision->ID'$right_checked /></th>\n";
681 $rows .= "\t<td>$date</td>\n";
682 $rows .= "\t<td>$name</td>\n";
683 $rows .= "\t<td class='action-links'>$actions</td>\n";
686 $title = sprintf( $titlef, $date, $name );
687 $rows .= "\t<li>$title</li>\n";
691 if ( 'form-table' == $format ) : ?>
693 <form action="revision.php" method="get">
695 <div class="tablenav">
696 <div class="alignleft">
697 <input type="submit" class="button-secondary" value="<?php _e( 'Compare Revisions' ); ?>" />
698 <input type="hidden" name="action" value="diff" />
704 <table class="widefat post-revisions">
706 <col style="width: 33%" />
707 <col style="width: 33%" />
708 <col style="width: 33%" />
711 <th scope="col"></th>
712 <th scope="col"><?php _e( 'Date Created' ); ?></th>
713 <th scope="col"><?php _e( 'Author' ); ?></th>
714 <th scope="col" class="action-links"><?php _e( 'Actions' ); ?></th>
728 echo "<ul class='post-revisions'>\n";