3 * WordPress Post Template Functions.
5 * Gets content for the current post in the loop.
12 * Display the ID of the current item in the WordPress Loop.
21 * Retrieve the ID of the current item in the WordPress Loop.
28 function get_the_ID() {
34 * Display or retrieve the current post title with optional content.
38 * @param string $before Optional. Content to prepend to the title.
39 * @param string $after Optional. Content to append to the title.
40 * @param bool $echo Optional, default to true.Whether to display or return.
41 * @return null|string Null on no title. String if $echo parameter is false.
43 function the_title($before = '', $after = '', $echo = true) {
44 $title = get_the_title();
46 if ( strlen($title) == 0 )
49 $title = $before . $title . $after;
58 * Sanitize the current title when retrieving or displaying.
60 * Works like {@link the_title()}, except the parameters can be in a string or
61 * an array. See the function for what can be override in the $args parameter.
63 * The title before it is displayed will have the tags stripped and {@link
64 * esc_attr()} before it is passed to the user or displayed. The default
65 * as with {@link the_title()}, is to display the title.
69 * @param string|array $args Optional. Override the defaults.
70 * @return string|null Null on failure or display. String when echo is false.
72 function the_title_attribute( $args = '' ) {
73 $title = get_the_title();
75 if ( strlen($title) == 0 )
78 $defaults = array('before' => '', 'after' => '', 'echo' => true);
79 $r = wp_parse_args($args, $defaults);
80 extract( $r, EXTR_SKIP );
82 $title = $before . $title . $after;
83 $title = esc_attr(strip_tags($title));
92 * Retrieve post title.
94 * If the post is protected and the visitor is not an admin, then "Protected"
95 * will be displayed before the post title. If the post is private, then
96 * "Private" will be located before the post title.
100 * @param int $id Optional. Post ID.
103 function get_the_title( $id = 0 ) {
104 $post = &get_post($id);
106 $title = isset($post->post_title) ? $post->post_title : '';
107 $id = isset($post->ID) ? $post->ID : (int) $id;
110 if ( !empty($post->post_password) ) {
111 $protected_title_format = apply_filters('protected_title_format', __('Protected: %s'));
112 $title = sprintf($protected_title_format, $title);
113 } else if ( isset($post->post_status) && 'private' == $post->post_status ) {
114 $private_title_format = apply_filters('private_title_format', __('Private: %s'));
115 $title = sprintf($private_title_format, $title);
118 return apply_filters( 'the_title', $title, $id );
122 * Display the Post Global Unique Identifier (guid).
124 * The guid will appear to be a link, but should not be used as an link to the
125 * post. The reason you should not use it as a link, is because of moving the
126 * blog across domains.
128 * Url is escaped to make it xml safe
132 * @param int $id Optional. Post ID.
134 function the_guid( $id = 0 ) {
135 echo esc_url( get_the_guid( $id ) );
139 * Retrieve the Post Global Unique Identifier (guid).
141 * The guid will appear to be a link, but should not be used as an link to the
142 * post. The reason you should not use it as a link, is because of moving the
143 * blog across domains.
147 * @param int $id Optional. Post ID.
150 function get_the_guid( $id = 0 ) {
151 $post = &get_post($id);
153 return apply_filters('get_the_guid', $post->guid);
157 * Display the post content.
161 * @param string $more_link_text Optional. Content for when there is more text.
162 * @param bool $stripteaser Optional. Strip teaser content before the more text. Default is false.
164 function the_content($more_link_text = null, $stripteaser = false) {
165 $content = get_the_content($more_link_text, $stripteaser);
166 $content = apply_filters('the_content', $content);
167 $content = str_replace(']]>', ']]>', $content);
172 * Retrieve the post content.
176 * @param string $more_link_text Optional. Content for when there is more text.
177 * @param bool $stripteaser Optional. Strip teaser content before the more text. Default is false.
180 function get_the_content($more_link_text = null, $stripteaser = false) {
181 global $post, $more, $page, $pages, $multipage, $preview;
183 if ( null === $more_link_text )
184 $more_link_text = __( '(more...)' );
189 // If post password required and it doesn't match the cookie.
190 if ( post_password_required($post) )
191 return get_the_password_form();
193 if ( $page > count($pages) ) // if the requested page doesn't exist
194 $page = count($pages); // give them the highest numbered page that DOES exist
196 $content = $pages[$page-1];
197 if ( preg_match('/<!--more(.*?)?-->/', $content, $matches) ) {
198 $content = explode($matches[0], $content, 2);
199 if ( !empty($matches[1]) && !empty($more_link_text) )
200 $more_link_text = strip_tags(wp_kses_no_null(trim($matches[1])));
204 $content = array($content);
206 if ( (false !== strpos($post->post_content, '<!--noteaser-->') && ((!$multipage) || ($page==1))) )
208 $teaser = $content[0];
209 if ( $more && $stripteaser && $hasTeaser )
212 if ( count($content) > 1 ) {
214 $output .= '<span id="more-' . $post->ID . '"></span>' . $content[1];
216 if ( ! empty($more_link_text) )
217 $output .= apply_filters( 'the_content_more_link', ' <a href="' . get_permalink() . "#more-{$post->ID}\" class=\"more-link\">$more_link_text</a>", $more_link_text );
218 $output = force_balance_tags($output);
222 if ( $preview ) // preview fix for javascript bug with foreign languages
223 $output = preg_replace_callback('/\%u([0-9A-F]{4})/', '_convert_urlencoded_to_entities', $output);
229 * Preview fix for javascript bug with foreign languages
233 * @param array $match Match array from preg_replace_callback
236 function _convert_urlencoded_to_entities( $match ) {
237 return '&#' . base_convert( $match[1], 16, 10 ) . ';';
241 * Display the post excerpt.
244 * @uses apply_filters() Calls 'the_excerpt' hook on post excerpt.
246 function the_excerpt() {
247 echo apply_filters('the_excerpt', get_the_excerpt());
251 * Retrieve the post excerpt.
255 * @param mixed $deprecated Not used.
258 function get_the_excerpt( $deprecated = '' ) {
259 if ( !empty( $deprecated ) )
260 _deprecated_argument( __FUNCTION__, '2.3' );
263 $output = $post->post_excerpt;
264 if ( post_password_required($post) ) {
265 $output = __('There is no excerpt because this is a protected post.');
269 return apply_filters('get_the_excerpt', $output);
273 * Whether post has excerpt.
277 * @param int $id Optional. Post ID.
280 function has_excerpt( $id = 0 ) {
281 $post = &get_post( $id );
282 return ( !empty( $post->post_excerpt ) );
286 * Display the classes for the post div.
290 * @param string|array $class One or more classes to add to the class list.
291 * @param int $post_id An optional post ID.
293 function post_class( $class = '', $post_id = null ) {
294 // Separates classes with a single space, collates classes for post DIV
295 echo 'class="' . join( ' ', get_post_class( $class, $post_id ) ) . '"';
299 * Retrieve the classes for the post div as an array.
301 * The class names are add are many. If the post is a sticky, then the 'sticky'
302 * class name. The class 'hentry' is always added to each post. For each
303 * category, the class will be added with 'category-' with category slug is
304 * added. The tags are the same way as the categories with 'tag-' before the tag
305 * slug. All classes are passed through the filter, 'post_class' with the list
306 * of classes, followed by $class parameter value, with the post ID as the last
311 * @param string|array $class One or more classes to add to the class list.
312 * @param int $post_id An optional post ID.
313 * @return array Array of classes.
315 function get_post_class( $class = '', $post_id = null ) {
316 $post = get_post($post_id);
323 $classes[] = 'post-' . $post->ID;
324 $classes[] = $post->post_type;
325 $classes[] = 'type-' . $post->post_type;
326 $classes[] = 'status-' . $post->post_status;
329 if ( post_type_supports( $post->post_type, 'post-formats' ) ) {
330 $post_format = get_post_format( $post->ID );
332 if ( $post_format && !is_wp_error($post_format) )
333 $classes[] = 'format-' . sanitize_html_class( $post_format );
335 $classes[] = 'format-standard';
338 // post requires password
339 if ( post_password_required($post->ID) )
340 $classes[] = 'post-password-required';
342 // sticky for Sticky Posts
343 if ( is_sticky($post->ID) && is_home() && !is_paged() )
344 $classes[] = 'sticky';
346 // hentry for hAtom compliance
347 $classes[] = 'hentry';
350 if ( is_object_in_taxonomy( $post->post_type, 'category' ) ) {
351 foreach ( (array) get_the_category($post->ID) as $cat ) {
352 if ( empty($cat->slug ) )
354 $classes[] = 'category-' . sanitize_html_class($cat->slug, $cat->term_id);
359 if ( is_object_in_taxonomy( $post->post_type, 'post_tag' ) ) {
360 foreach ( (array) get_the_tags($post->ID) as $tag ) {
361 if ( empty($tag->slug ) )
363 $classes[] = 'tag-' . sanitize_html_class($tag->slug, $tag->term_id);
367 if ( !empty($class) ) {
368 if ( !is_array( $class ) )
369 $class = preg_split('#\s+#', $class);
370 $classes = array_merge($classes, $class);
373 $classes = array_map('esc_attr', $classes);
375 return apply_filters('post_class', $classes, $class, $post->ID);
379 * Display the classes for the body element.
383 * @param string|array $class One or more classes to add to the class list.
385 function body_class( $class = '' ) {
386 // Separates classes with a single space, collates classes for body element
387 echo 'class="' . join( ' ', get_body_class( $class ) ) . '"';
391 * Retrieve the classes for the body element as an array.
395 * @param string|array $class One or more classes to add to the class list.
396 * @return array Array of classes.
398 function get_body_class( $class = '' ) {
399 global $wp_query, $wpdb;
406 if ( is_front_page() )
411 $classes[] = 'archive';
415 $classes[] = 'search';
416 $classes[] = $wp_query->posts ? 'search-results' : 'search-no-results';
419 $classes[] = 'paged';
420 if ( is_attachment() )
421 $classes[] = 'attachment';
423 $classes[] = 'error404';
426 $post_id = $wp_query->get_queried_object_id();
427 $post = $wp_query->get_queried_object();
429 $classes[] = 'single';
430 $classes[] = 'single-' . sanitize_html_class($post->post_type, $post_id);
431 $classes[] = 'postid-' . $post_id;
434 if ( post_type_supports( $post->post_type, 'post-formats' ) ) {
435 $post_format = get_post_format( $post->ID );
437 if ( $post_format && !is_wp_error($post_format) )
438 $classes[] = 'single-format-' . sanitize_html_class( $post_format );
440 $classes[] = 'single-format-standard';
443 if ( is_attachment() ) {
444 $mime_type = get_post_mime_type($post_id);
445 $mime_prefix = array( 'application/', 'image/', 'text/', 'audio/', 'video/', 'music/' );
446 $classes[] = 'attachmentid-' . $post_id;
447 $classes[] = 'attachment-' . str_replace( $mime_prefix, '', $mime_type );
449 } elseif ( is_archive() ) {
450 if ( is_post_type_archive() ) {
451 $classes[] = 'post-type-archive';
452 $classes[] = 'post-type-archive-' . sanitize_html_class( get_query_var( 'post_type' ) );
453 } else if ( is_author() ) {
454 $author = $wp_query->get_queried_object();
455 $classes[] = 'author';
456 $classes[] = 'author-' . sanitize_html_class( $author->user_nicename , $author->ID );
457 $classes[] = 'author-' . $author->ID;
458 } elseif ( is_category() ) {
459 $cat = $wp_query->get_queried_object();
460 $classes[] = 'category';
461 $classes[] = 'category-' . sanitize_html_class( $cat->slug, $cat->term_id );
462 $classes[] = 'category-' . $cat->term_id;
463 } elseif ( is_tag() ) {
464 $tags = $wp_query->get_queried_object();
466 $classes[] = 'tag-' . sanitize_html_class( $tags->slug, $tags->term_id );
467 $classes[] = 'tag-' . $tags->term_id;
468 } elseif ( is_tax() ) {
469 $term = $wp_query->get_queried_object();
470 $classes[] = 'tax-' . sanitize_html_class( $term->taxonomy );
471 $classes[] = 'term-' . sanitize_html_class( $term->slug, $term->term_id );
472 $classes[] = 'term-' . $term->term_id;
474 } elseif ( is_page() ) {
477 $page_id = $wp_query->get_queried_object_id();
479 $post = get_page($page_id);
481 $classes[] = 'page-id-' . $page_id;
483 if ( $wpdb->get_var( $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_parent = %d AND post_type = 'page' AND post_status = 'publish' LIMIT 1", $page_id) ) )
484 $classes[] = 'page-parent';
486 if ( $post->post_parent ) {
487 $classes[] = 'page-child';
488 $classes[] = 'parent-pageid-' . $post->post_parent;
490 if ( is_page_template() ) {
491 $classes[] = 'page-template';
492 $classes[] = 'page-template-' . sanitize_html_class( str_replace( '.', '-', get_page_template_slug( $page_id ) ) );
494 $classes[] = 'page-template-default';
498 if ( is_user_logged_in() )
499 $classes[] = 'logged-in';
501 if ( is_admin_bar_showing() )
502 $classes[] = 'admin-bar';
504 if ( get_theme_mod( 'background_color' ) || get_background_image() )
505 $classes[] = 'custom-background';
507 $page = $wp_query->get( 'page' );
509 if ( !$page || $page < 2)
510 $page = $wp_query->get( 'paged' );
512 if ( $page && $page > 1 ) {
513 $classes[] = 'paged-' . $page;
516 $classes[] = 'single-paged-' . $page;
518 $classes[] = 'page-paged-' . $page;
519 elseif ( is_category() )
520 $classes[] = 'category-paged-' . $page;
522 $classes[] = 'tag-paged-' . $page;
524 $classes[] = 'date-paged-' . $page;
525 elseif ( is_author() )
526 $classes[] = 'author-paged-' . $page;
527 elseif ( is_search() )
528 $classes[] = 'search-paged-' . $page;
529 elseif ( is_post_type_archive() )
530 $classes[] = 'post-type-paged-' . $page;
533 if ( ! empty( $class ) ) {
534 if ( !is_array( $class ) )
535 $class = preg_split( '#\s+#', $class );
536 $classes = array_merge( $classes, $class );
538 // Ensure that we always coerce class to being an array.
542 $classes = array_map( 'esc_attr', $classes );
544 return apply_filters( 'body_class', $classes, $class );
548 * Whether post requires password and correct password has been provided.
552 * @param int|object $post An optional post. Global $post used if not provided.
553 * @return bool false if a password is not required or the correct password cookie is present, true otherwise.
555 function post_password_required( $post = null ) {
558 $post = get_post($post);
560 if ( empty( $post->post_password ) )
563 if ( ! isset( $_COOKIE['wp-postpass_' . COOKIEHASH] ) )
566 if ( empty( $wp_hasher ) ) {
567 require_once( ABSPATH . 'wp-includes/class-phpass.php');
568 // By default, use the portable hash from phpass
569 $wp_hasher = new PasswordHash(8, true);
572 $hash = stripslashes( $_COOKIE[ 'wp-postpass_' . COOKIEHASH ] );
574 return ! $wp_hasher->CheckPassword( $post->post_password, $hash );
578 * Display "sticky" CSS class, if a post is sticky.
582 * @param int $post_id An optional post ID.
584 function sticky_class( $post_id = null ) {
585 if ( !is_sticky($post_id) )
592 * Page Template Functions for usage in Themes
595 * @subpackage Template
599 * The formatted output of a list of pages.
601 * Displays page links for paginated posts (i.e. includes the <!--nextpage-->.
602 * Quicktag one or more times). This tag must be within The Loop.
604 * The defaults for overwriting are:
605 * 'next_or_number' - Default is 'number' (string). Indicates whether page
606 * numbers should be used. Valid values are number and next.
607 * 'nextpagelink' - Default is 'Next Page' (string). Text for link to next page.
609 * 'previouspagelink' - Default is 'Previous Page' (string). Text for link to
610 * previous page, if available.
611 * 'pagelink' - Default is '%' (String).Format string for page numbers. The % in
612 * the parameter string will be replaced with the page number, so Page %
613 * generates "Page 1", "Page 2", etc. Defaults to %, just the page number.
614 * 'before' - Default is '<p> Pages:' (string). The html or text to prepend to
616 * 'after' - Default is '</p>' (string). The html or text to append to each
618 * 'link_before' - Default is '' (string). The html or text to prepend to each
619 * Pages link inside the <a> tag. Also prepended to the current item, which
621 * 'link_after' - Default is '' (string). The html or text to append to each
622 * Pages link inside the <a> tag. Also appended to the current item, which
628 * @param string|array $args Optional. Overwrite the defaults.
629 * @return string Formatted output in HTML.
631 function wp_link_pages($args = '') {
633 'before' => '<p>' . __('Pages:'), 'after' => '</p>',
634 'link_before' => '', 'link_after' => '',
635 'next_or_number' => 'number', 'nextpagelink' => __('Next page'),
636 'previouspagelink' => __('Previous page'), 'pagelink' => '%',
640 $r = wp_parse_args( $args, $defaults );
641 $r = apply_filters( 'wp_link_pages_args', $r );
642 extract( $r, EXTR_SKIP );
644 global $page, $numpages, $multipage, $more, $pagenow;
648 if ( 'number' == $next_or_number ) {
650 for ( $i = 1; $i < ($numpages+1); $i = $i + 1 ) {
651 $j = str_replace('%',$i,$pagelink);
653 if ( ($i != $page) || ((!$more) && ($page==1)) ) {
654 $output .= _wp_link_page($i);
656 $output .= $link_before . $j . $link_after;
657 if ( ($i != $page) || ((!$more) && ($page==1)) )
666 $output .= _wp_link_page($i);
667 $output .= $link_before. $previouspagelink . $link_after . '</a>';
670 if ( $i <= $numpages && $more ) {
671 $output .= _wp_link_page($i);
672 $output .= $link_before. $nextpagelink . $link_after . '</a>';
686 * Helper function for wp_link_pages().
691 * @param int $i Page number.
692 * @return string Link.
694 function _wp_link_page( $i ) {
695 global $post, $wp_rewrite;
698 $url = get_permalink();
700 if ( '' == get_option('permalink_structure') || in_array($post->post_status, array('draft', 'pending')) )
701 $url = add_query_arg( 'page', $i, get_permalink() );
702 elseif ( 'page' == get_option('show_on_front') && get_option('page_on_front') == $post->ID )
703 $url = trailingslashit(get_permalink()) . user_trailingslashit("$wp_rewrite->pagination_base/" . $i, 'single_paged');
705 $url = trailingslashit(get_permalink()) . user_trailingslashit($i, 'single_paged');
708 return '<a href="' . esc_url( $url ) . '">';
712 // Post-meta: Custom per-post fields.
716 * Retrieve post custom meta data field.
720 * @param string $key Meta data key name.
721 * @return bool|string|array Array of values or single value, if only one element exists. False will be returned if key does not exist.
723 function post_custom( $key = '' ) {
724 $custom = get_post_custom();
726 if ( !isset( $custom[$key] ) )
728 elseif ( 1 == count($custom[$key]) )
729 return $custom[$key][0];
731 return $custom[$key];
735 * Display list of post custom fields.
737 * @internal This will probably change at some point...
739 * @uses apply_filters() Calls 'the_meta_key' on list item HTML content, with key and value as separate parameters.
741 function the_meta() {
742 if ( $keys = get_post_custom_keys() ) {
743 echo "<ul class='post-meta'>\n";
744 foreach ( (array) $keys as $key ) {
746 if ( is_protected_meta( $keyt, 'post' ) )
748 $values = array_map('trim', get_post_custom_values($key));
749 $value = implode($values,', ');
750 echo apply_filters('the_meta_key', "<li><span class='post-meta-key'>$key:</span> $value</li>\n", $key, $value);
761 * Retrieve or display list of pages as a dropdown (select list).
765 * @param array|string $args Optional. Override default arguments.
766 * @return string HTML content, if not displaying.
768 function wp_dropdown_pages($args = '') {
770 'depth' => 0, 'child_of' => 0,
771 'selected' => 0, 'echo' => 1,
772 'name' => 'page_id', 'id' => '',
773 'show_option_none' => '', 'show_option_no_change' => '',
774 'option_none_value' => ''
777 $r = wp_parse_args( $args, $defaults );
778 extract( $r, EXTR_SKIP );
780 $pages = get_pages($r);
782 // Back-compat with old system where both id and name were based on $name argument
786 if ( ! empty($pages) ) {
787 $output = "<select name='" . esc_attr( $name ) . "' id='" . esc_attr( $id ) . "'>\n";
788 if ( $show_option_no_change )
789 $output .= "\t<option value=\"-1\">$show_option_no_change</option>";
790 if ( $show_option_none )
791 $output .= "\t<option value=\"" . esc_attr($option_none_value) . "\">$show_option_none</option>\n";
792 $output .= walk_page_dropdown_tree($pages, $depth, $r);
793 $output .= "</select>\n";
796 $output = apply_filters('wp_dropdown_pages', $output);
805 * Retrieve or display list of pages in list (li) format.
809 * @param array|string $args Optional. Override default arguments.
810 * @return string HTML content, if not displaying.
812 function wp_list_pages($args = '') {
814 'depth' => 0, 'show_date' => '',
815 'date_format' => get_option('date_format'),
816 'child_of' => 0, 'exclude' => '',
817 'title_li' => __('Pages'), 'echo' => 1,
818 'authors' => '', 'sort_column' => 'menu_order, post_title',
819 'link_before' => '', 'link_after' => '', 'walker' => '',
822 $r = wp_parse_args( $args, $defaults );
823 extract( $r, EXTR_SKIP );
828 // sanitize, mostly to keep spaces out
829 $r['exclude'] = preg_replace('/[^0-9,]/', '', $r['exclude']);
831 // Allow plugins to filter an array of excluded pages (but don't put a nullstring into the array)
832 $exclude_array = ( $r['exclude'] ) ? explode(',', $r['exclude']) : array();
833 $r['exclude'] = implode( ',', apply_filters('wp_list_pages_excludes', $exclude_array) );
836 $r['hierarchical'] = 0;
837 $pages = get_pages($r);
839 if ( !empty($pages) ) {
840 if ( $r['title_li'] )
841 $output .= '<li class="pagenav">' . $r['title_li'] . '<ul>';
844 if ( is_page() || is_attachment() || $wp_query->is_posts_page )
845 $current_page = $wp_query->get_queried_object_id();
846 $output .= walk_page_tree($pages, $r['depth'], $current_page, $r);
848 if ( $r['title_li'] )
849 $output .= '</ul></li>';
852 $output = apply_filters('wp_list_pages', $output, $r);
861 * Display or retrieve list of pages with optional home link.
863 * The arguments are listed below and part of the arguments are for {@link
864 * wp_list_pages()} function. Check that function for more info on those
868 * <li><strong>sort_column</strong> - How to sort the list of pages. Defaults
869 * to page title. Use column for posts table.</li>
870 * <li><strong>menu_class</strong> - Class to use for the div ID which contains
871 * the page list. Defaults to 'menu'.</li>
872 * <li><strong>echo</strong> - Whether to echo list or return it. Defaults to
874 * <li><strong>link_before</strong> - Text before show_home argument text.</li>
875 * <li><strong>link_after</strong> - Text after show_home argument text.</li>
876 * <li><strong>show_home</strong> - If you set this argument, then it will
877 * display the link to the home page. The show_home argument really just needs
878 * to be set to the value of the text of the link.</li>
883 * @param array|string $args
885 function wp_page_menu( $args = array() ) {
886 $defaults = array('sort_column' => 'menu_order, post_title', 'menu_class' => 'menu', 'echo' => true, 'link_before' => '', 'link_after' => '');
887 $args = wp_parse_args( $args, $defaults );
888 $args = apply_filters( 'wp_page_menu_args', $args );
894 // Show Home in the menu
895 if ( ! empty($args['show_home']) ) {
896 if ( true === $args['show_home'] || '1' === $args['show_home'] || 1 === $args['show_home'] )
899 $text = $args['show_home'];
901 if ( is_front_page() && !is_paged() )
902 $class = 'class="current_page_item"';
903 $menu .= '<li ' . $class . '><a href="' . home_url( '/' ) . '" title="' . esc_attr($text) . '">' . $args['link_before'] . $text . $args['link_after'] . '</a></li>';
904 // If the front page is a page, add it to the exclude list
905 if (get_option('show_on_front') == 'page') {
906 if ( !empty( $list_args['exclude'] ) ) {
907 $list_args['exclude'] .= ',';
909 $list_args['exclude'] = '';
911 $list_args['exclude'] .= get_option('page_on_front');
915 $list_args['echo'] = false;
916 $list_args['title_li'] = '';
917 $menu .= str_replace( array( "\r", "\n", "\t" ), '', wp_list_pages($list_args) );
920 $menu = '<ul>' . $menu . '</ul>';
922 $menu = '<div class="' . esc_attr($args['menu_class']) . '">' . $menu . "</div>\n";
923 $menu = apply_filters( 'wp_page_menu', $menu, $args );
935 * Retrieve HTML list content for page list.
937 * @uses Walker_Page to create HTML list content.
939 * @see Walker_Page::walk() for parameters and return description.
941 function walk_page_tree($pages, $depth, $current_page, $r) {
942 if ( empty($r['walker']) )
943 $walker = new Walker_Page;
945 $walker = $r['walker'];
947 $args = array($pages, $depth, $r, $current_page);
948 return call_user_func_array(array(&$walker, 'walk'), $args);
952 * Retrieve HTML dropdown (select) content for page list.
954 * @uses Walker_PageDropdown to create HTML dropdown content.
956 * @see Walker_PageDropdown::walk() for parameters and return description.
958 function walk_page_dropdown_tree() {
959 $args = func_get_args();
960 if ( empty($args[2]['walker']) ) // the user's options are the third parameter
961 $walker = new Walker_PageDropdown;
963 $walker = $args[2]['walker'];
965 return call_user_func_array(array(&$walker, 'walk'), $args);
969 * Create HTML list of pages.
975 class Walker_Page extends Walker {
977 * @see Walker::$tree_type
981 var $tree_type = 'page';
984 * @see Walker::$db_fields
986 * @todo Decouple this.
989 var $db_fields = array ('parent' => 'post_parent', 'id' => 'ID');
992 * @see Walker::start_lvl()
995 * @param string $output Passed by reference. Used to append additional content.
996 * @param int $depth Depth of page. Used for padding.
998 function start_lvl( &$output, $depth = 0, $args = array() ) {
999 $indent = str_repeat("\t", $depth);
1000 $output .= "\n$indent<ul class='children'>\n";
1004 * @see Walker::end_lvl()
1007 * @param string $output Passed by reference. Used to append additional content.
1008 * @param int $depth Depth of page. Used for padding.
1010 function end_lvl( &$output, $depth = 0, $args = array() ) {
1011 $indent = str_repeat("\t", $depth);
1012 $output .= "$indent</ul>\n";
1016 * @see Walker::start_el()
1019 * @param string $output Passed by reference. Used to append additional content.
1020 * @param object $page Page data object.
1021 * @param int $depth Depth of page. Used for padding.
1022 * @param int $current_page Page ID.
1023 * @param array $args
1025 function start_el( &$output, $page, $depth, $args, $current_page = 0 ) {
1027 $indent = str_repeat("\t", $depth);
1031 extract($args, EXTR_SKIP);
1032 $css_class = array('page_item', 'page-item-'.$page->ID);
1033 if ( !empty($current_page) ) {
1034 $_current_page = get_page( $current_page );
1035 _get_post_ancestors($_current_page);
1036 if ( isset($_current_page->ancestors) && in_array($page->ID, (array) $_current_page->ancestors) )
1037 $css_class[] = 'current_page_ancestor';
1038 if ( $page->ID == $current_page )
1039 $css_class[] = 'current_page_item';
1040 elseif ( $_current_page && $page->ID == $_current_page->post_parent )
1041 $css_class[] = 'current_page_parent';
1042 } elseif ( $page->ID == get_option('page_for_posts') ) {
1043 $css_class[] = 'current_page_parent';
1046 $css_class = implode( ' ', apply_filters( 'page_css_class', $css_class, $page, $depth, $args, $current_page ) );
1048 $output .= $indent . '<li class="' . $css_class . '"><a href="' . get_permalink($page->ID) . '">' . $link_before . apply_filters( 'the_title', $page->post_title, $page->ID ) . $link_after . '</a>';
1050 if ( !empty($show_date) ) {
1051 if ( 'modified' == $show_date )
1052 $time = $page->post_modified;
1054 $time = $page->post_date;
1056 $output .= " " . mysql2date($date_format, $time);
1061 * @see Walker::end_el()
1064 * @param string $output Passed by reference. Used to append additional content.
1065 * @param object $page Page data object. Not used.
1066 * @param int $depth Depth of page. Not Used.
1068 function end_el( &$output, $page, $depth = 0, $args = array() ) {
1069 $output .= "</li>\n";
1075 * Create HTML dropdown list of pages.
1077 * @package WordPress
1081 class Walker_PageDropdown extends Walker {
1083 * @see Walker::$tree_type
1087 var $tree_type = 'page';
1090 * @see Walker::$db_fields
1092 * @todo Decouple this
1095 var $db_fields = array ('parent' => 'post_parent', 'id' => 'ID');
1098 * @see Walker::start_el()
1101 * @param string $output Passed by reference. Used to append additional content.
1102 * @param object $page Page data object.
1103 * @param int $depth Depth of page in reference to parent pages. Used for padding.
1104 * @param array $args Uses 'selected' argument for selected page to set selected HTML attribute for option element.
1106 function start_el(&$output, $page, $depth, $args, $id = 0) {
1107 $pad = str_repeat(' ', $depth * 3);
1109 $output .= "\t<option class=\"level-$depth\" value=\"$page->ID\"";
1110 if ( $page->ID == $args['selected'] )
1111 $output .= ' selected="selected"';
1113 $title = apply_filters( 'list_pages', $page->post_title, $page );
1114 $output .= $pad . esc_html( $title );
1115 $output .= "</option>\n";
1124 * Display an attachment page link using an image or icon.
1128 * @param int $id Optional. Post ID.
1129 * @param bool $fullsize Optional, default is false. Whether to use full size.
1130 * @param bool $deprecated Deprecated. Not used.
1131 * @param bool $permalink Optional, default is false. Whether to include permalink.
1133 function the_attachment_link( $id = 0, $fullsize = false, $deprecated = false, $permalink = false ) {
1134 if ( !empty( $deprecated ) )
1135 _deprecated_argument( __FUNCTION__, '2.5' );
1138 echo wp_get_attachment_link($id, 'full', $permalink);
1140 echo wp_get_attachment_link($id, 'thumbnail', $permalink);
1144 * Retrieve an attachment page link using an image or icon, if possible.
1147 * @uses apply_filters() Calls 'wp_get_attachment_link' filter on HTML content with same parameters as function.
1149 * @param int $id Optional. Post ID.
1150 * @param string $size Optional, default is 'thumbnail'. Size of image, either array or string.
1151 * @param bool $permalink Optional, default is false. Whether to add permalink to image.
1152 * @param bool $icon Optional, default is false. Whether to include icon.
1153 * @param string $text Optional, default is false. If string, then will be link text.
1154 * @return string HTML content.
1156 function wp_get_attachment_link( $id = 0, $size = 'thumbnail', $permalink = false, $icon = false, $text = false ) {
1157 $id = intval( $id );
1158 $_post = & get_post( $id );
1160 if ( empty( $_post ) || ( 'attachment' != $_post->post_type ) || ! $url = wp_get_attachment_url( $_post->ID ) )
1161 return __( 'Missing Attachment' );
1164 $url = get_attachment_link( $_post->ID );
1166 $post_title = esc_attr( $_post->post_title );
1170 elseif ( $size && 'none' != $size )
1171 $link_text = wp_get_attachment_image( $id, $size, $icon );
1175 if ( trim( $link_text ) == '' )
1176 $link_text = $_post->post_title;
1178 return apply_filters( 'wp_get_attachment_link', "<a href='$url' title='$post_title'>$link_text</a>", $id, $size, $permalink, $icon, $text );
1182 * Wrap attachment in <<p>> element before content.
1185 * @uses apply_filters() Calls 'prepend_attachment' hook on HTML content.
1187 * @param string $content
1190 function prepend_attachment($content) {
1193 if ( empty($post->post_type) || $post->post_type != 'attachment' )
1196 $p = '<p class="attachment">';
1197 // show the medium sized image representation of the attachment if available, and link to the raw file
1198 $p .= wp_get_attachment_link(0, 'medium', false);
1200 $p = apply_filters('prepend_attachment', $p);
1202 return "$p\n$content";
1210 * Retrieve protected post password form content.
1213 * @uses apply_filters() Calls 'the_password_form' filter on output.
1215 * @return string HTML content for password form for password protected post.
1217 function get_the_password_form() {
1219 $label = 'pwbox-' . ( empty($post->ID) ? rand() : $post->ID );
1220 $output = '<form action="' . esc_url( site_url( 'wp-login.php?action=postpass', 'login_post' ) ) . '" method="post">
1221 <p>' . __("This post is password protected. To view it please enter your password below:") . '</p>
1222 <p><label for="' . $label . '">' . __("Password:") . ' <input name="post_password" id="' . $label . '" type="password" size="20" /></label> <input type="submit" name="Submit" value="' . esc_attr__("Submit") . '" /></p>
1225 return apply_filters('the_password_form', $output);
1229 * Whether currently in a page template.
1231 * This template tag allows you to determine if you are in a page template.
1232 * You can optionally provide a template name and then the check will be
1233 * specific to that template.
1238 * @param string $template The specific template name if specific matching is required.
1239 * @return bool False on failure, true if success.
1241 function is_page_template( $template = '' ) {
1245 $page_template = get_page_template_slug( get_queried_object_id() );
1247 if ( empty( $template ) )
1248 return (bool) $page_template;
1250 if ( $template == $page_template )
1253 if ( 'default' == $template && ! $page_template )
1260 * Get the specific template name for a page.
1264 * @param int $id The page ID to check. Defaults to the current post, when used in the loop.
1265 * @return string|bool Page template filename. Returns an empty string when the default page template
1266 * is in use. Returns false if the post is not a page.
1268 function get_page_template_slug( $post_id = null ) {
1269 $post = get_post( $post_id );
1270 if ( 'page' != $post->post_type )
1272 $template = get_post_meta( $post->ID, '_wp_page_template', true );
1273 if ( ! $template || 'default' == $template )
1279 * Retrieve formatted date timestamp of a revision (linked to that revisions's page).
1281 * @package WordPress
1282 * @subpackage Post_Revisions
1287 * @param int|object $revision Revision ID or revision object.
1288 * @param bool $link Optional, default is true. Link to revisions's page?
1289 * @return string i18n formatted datetimestamp or localized 'Current Revision'.
1291 function wp_post_revision_title( $revision, $link = true ) {
1292 if ( !$revision = get_post( $revision ) )
1295 if ( !in_array( $revision->post_type, array( 'post', 'page', 'revision' ) ) )
1298 /* translators: revision date format, see http://php.net/date */
1299 $datef = _x( 'j F, Y @ G:i', 'revision date format');
1300 /* translators: 1: date */
1301 $autosavef = __( '%1$s [Autosave]' );
1302 /* translators: 1: date */
1303 $currentf = __( '%1$s [Current Revision]' );
1305 $date = date_i18n( $datef, strtotime( $revision->post_modified ) );
1306 if ( $link && current_user_can( 'edit_post', $revision->ID ) && $link = get_edit_post_link( $revision->ID ) )
1307 $date = "<a href='$link'>$date</a>";
1309 if ( !wp_is_post_revision( $revision ) )
1310 $date = sprintf( $currentf, $date );
1311 elseif ( wp_is_post_autosave( $revision ) )
1312 $date = sprintf( $autosavef, $date );
1318 * Display list of a post's revisions.
1320 * Can output either a UL with edit links or a TABLE with diff interface, and
1321 * restore action links.
1323 * Second argument controls parameters:
1324 * (bool) parent : include the parent (the "Current Revision") in the list.
1325 * (string) format : 'list' or 'form-table'. 'list' outputs UL, 'form-table'
1326 * outputs TABLE with UI.
1327 * (int) right : what revision is currently being viewed - used in
1328 * form-table format.
1329 * (int) left : what revision is currently being diffed against right -
1330 * used in form-table format.
1332 * @package WordPress
1333 * @subpackage Post_Revisions
1336 * @uses wp_get_post_revisions()
1337 * @uses wp_post_revision_title()
1338 * @uses get_edit_post_link()
1339 * @uses get_the_author_meta()
1341 * @todo split into two functions (list, form-table) ?
1343 * @param int|object $post_id Post ID or post object.
1344 * @param string|array $args See description {@link wp_parse_args()}.
1347 function wp_list_post_revisions( $post_id = 0, $args = null ) {
1348 if ( !$post = get_post( $post_id ) )
1351 $defaults = array( 'parent' => false, 'right' => false, 'left' => false, 'format' => 'list', 'type' => 'all' );
1352 extract( wp_parse_args( $args, $defaults ), EXTR_SKIP );
1356 if ( !$autosave = wp_get_post_autosave( $post->ID ) )
1358 $revisions = array( $autosave );
1360 case 'revision' : // just revisions - remove autosave later
1363 if ( !$revisions = wp_get_post_revisions( $post->ID ) )
1368 /* translators: post revision: 1: when, 2: author name */
1369 $titlef = _x( '%1$s by %2$s', 'post revision' );
1372 array_unshift( $revisions, $post );
1374 $rows = $right_checked = '';
1376 $can_edit_post = current_user_can( 'edit_post', $post->ID );
1377 foreach ( $revisions as $revision ) {
1378 if ( !current_user_can( 'read_post', $revision->ID ) )
1380 if ( 'revision' === $type && wp_is_post_autosave( $revision ) )
1383 $date = wp_post_revision_title( $revision );
1384 $name = get_the_author_meta( 'display_name', $revision->post_author );
1386 if ( 'form-table' == $format ) {
1388 $left_checked = $left == $revision->ID ? ' checked="checked"' : '';
1390 $left_checked = $right_checked ? ' checked="checked"' : ''; // [sic] (the next one)
1391 $right_checked = $right == $revision->ID ? ' checked="checked"' : '';
1393 $class = $class ? '' : " class='alternate'";
1395 if ( $post->ID != $revision->ID && $can_edit_post )
1396 $actions = '<a href="' . wp_nonce_url( add_query_arg( array( 'revision' => $revision->ID, 'action' => 'restore' ) ), "restore-post_$post->ID|$revision->ID" ) . '">' . __( 'Restore' ) . '</a>';
1400 $rows .= "<tr$class>\n";
1401 $rows .= "\t<th style='white-space: nowrap' scope='row'><input type='radio' name='left' value='$revision->ID'$left_checked /></th>\n";
1402 $rows .= "\t<th style='white-space: nowrap' scope='row'><input type='radio' name='right' value='$revision->ID'$right_checked /></th>\n";
1403 $rows .= "\t<td>$date</td>\n";
1404 $rows .= "\t<td>$name</td>\n";
1405 $rows .= "\t<td class='action-links'>$actions</td>\n";
1408 $title = sprintf( $titlef, $date, $name );
1409 $rows .= "\t<li>$title</li>\n";
1413 if ( 'form-table' == $format ) : ?>
1415 <form action="revision.php" method="get">
1417 <div class="tablenav">
1418 <div class="alignleft">
1419 <input type="submit" class="button-secondary" value="<?php esc_attr_e( 'Compare Revisions' ); ?>" />
1420 <input type="hidden" name="action" value="diff" />
1421 <input type="hidden" name="post_type" value="<?php echo esc_attr($post->post_type); ?>" />
1425 <br class="clear" />
1427 <table class="widefat post-revisions" cellspacing="0" id="post-revisions">
1430 <col style="width: 33%" />
1431 <col style="width: 33%" />
1432 <col style="width: 33%" />
1435 <th scope="col"><?php /* translators: column name in revisons */ _ex( 'Old', 'revisions column name' ); ?></th>
1436 <th scope="col"><?php /* translators: column name in revisons */ _ex( 'New', 'revisions column name' ); ?></th>
1437 <th scope="col"><?php /* translators: column name in revisons */ _ex( 'Date Created', 'revisions column name' ); ?></th>
1438 <th scope="col"><?php _e( 'Author' ); ?></th>
1439 <th scope="col" class="action-links"><?php _e( 'Actions' ); ?></th>
1444 <?php echo $rows; ?>
1453 echo "<ul class='post-revisions'>\n";