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() {
29 return get_post()->ID;
33 * Display or retrieve the current post title with optional content.
37 * @param string $before Optional. Content to prepend to the title.
38 * @param string $after Optional. Content to append to the title.
39 * @param bool $echo Optional, default to true.Whether to display or return.
40 * @return null|string Null on no title. String if $echo parameter is false.
42 function the_title($before = '', $after = '', $echo = true) {
43 $title = get_the_title();
45 if ( strlen($title) == 0 )
48 $title = $before . $title . $after;
57 * Sanitize the current title when retrieving or displaying.
59 * Works like {@link the_title()}, except the parameters can be in a string or
60 * an array. See the function for what can be override in the $args parameter.
62 * The title before it is displayed will have the tags stripped and {@link
63 * esc_attr()} before it is passed to the user or displayed. The default
64 * as with {@link the_title()}, is to display the title.
68 * @param string|array $args Optional. Override the defaults.
69 * @return string|null Null on failure or display. String when echo is false.
71 function the_title_attribute( $args = '' ) {
72 $defaults = array('before' => '', 'after' => '', 'echo' => true, 'post' => get_post() );
73 $r = wp_parse_args($args, $defaults);
74 extract( $r, EXTR_SKIP );
76 $title = get_the_title( $post );
78 if ( strlen($title) == 0 )
81 $title = $before . $title . $after;
82 $title = esc_attr(strip_tags($title));
91 * Retrieve post title.
93 * If the post is protected and the visitor is not an admin, then "Protected"
94 * will be displayed before the post title. If the post is private, then
95 * "Private" will be located before the post title.
99 * @param int|object $post Optional. Post ID or object.
102 function get_the_title( $post = 0 ) {
103 $post = get_post( $post );
105 $title = isset( $post->post_title ) ? $post->post_title : '';
106 $id = isset( $post->ID ) ? $post->ID : 0;
108 if ( ! is_admin() ) {
109 if ( ! empty( $post->post_password ) ) {
110 $protected_title_format = apply_filters( 'protected_title_format', __( 'Protected: %s' ) );
111 $title = sprintf( $protected_title_format, $title );
112 } else if ( isset( $post->post_status ) && 'private' == $post->post_status ) {
113 $private_title_format = apply_filters( 'private_title_format', __( 'Private: %s' ) );
114 $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 $strip_teaser Optional. Strip teaser content before the more text. Default is false.
164 function the_content( $more_link_text = null, $strip_teaser = false) {
165 $content = get_the_content( $more_link_text, $strip_teaser );
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, $strip_teaser = false ) {
181 global $page, $more, $preview, $pages, $multipage;
185 if ( null === $more_link_text )
186 $more_link_text = __( '(more…)' );
191 // If post password required and it doesn't match the cookie.
192 if ( post_password_required( $post ) )
193 return get_the_password_form( $post );
195 if ( $page > count( $pages ) ) // if the requested page doesn't exist
196 $page = count( $pages ); // give them the highest numbered page that DOES exist
198 $content = $pages[$page - 1];
199 if ( preg_match( '/<!--more(.*?)?-->/', $content, $matches ) ) {
200 $content = explode( $matches[0], $content, 2 );
201 if ( ! empty( $matches[1] ) && ! empty( $more_link_text ) )
202 $more_link_text = strip_tags( wp_kses_no_null( trim( $matches[1] ) ) );
206 $content = array( $content );
209 if ( false !== strpos( $post->post_content, '<!--noteaser-->' ) && ( ! $multipage || $page == 1 ) )
210 $strip_teaser = true;
212 $teaser = $content[0];
214 if ( $more && $strip_teaser && $has_teaser )
219 if ( count( $content ) > 1 ) {
221 $output .= '<span id="more-' . $post->ID . '"></span>' . $content[1];
223 if ( ! empty( $more_link_text ) )
224 $output .= apply_filters( 'the_content_more_link', ' <a href="' . get_permalink() . "#more-{$post->ID}\" class=\"more-link\">$more_link_text</a>", $more_link_text );
225 $output = force_balance_tags( $output );
229 if ( $preview ) // preview fix for javascript bug with foreign languages
230 $output = preg_replace_callback( '/\%u([0-9A-F]{4})/', '_convert_urlencoded_to_entities', $output );
236 * Preview fix for javascript bug with foreign languages
240 * @param array $match Match array from preg_replace_callback
243 function _convert_urlencoded_to_entities( $match ) {
244 return '&#' . base_convert( $match[1], 16, 10 ) . ';';
248 * Display the post excerpt.
251 * @uses apply_filters() Calls 'the_excerpt' hook on post excerpt.
253 function the_excerpt() {
254 echo apply_filters('the_excerpt', get_the_excerpt());
258 * Retrieve the post excerpt.
262 * @param mixed $deprecated Not used.
265 function get_the_excerpt( $deprecated = '' ) {
266 if ( !empty( $deprecated ) )
267 _deprecated_argument( __FUNCTION__, '2.3' );
271 if ( post_password_required() ) {
272 return __( 'There is no excerpt because this is a protected post.' );
275 return apply_filters( 'get_the_excerpt', $post->post_excerpt );
279 * Whether post has excerpt.
283 * @param int $id Optional. Post ID.
286 function has_excerpt( $id = 0 ) {
287 $post = get_post( $id );
288 return ( !empty( $post->post_excerpt ) );
292 * Display the classes for the post div.
296 * @param string|array $class One or more classes to add to the class list.
297 * @param int $post_id An optional post ID.
299 function post_class( $class = '', $post_id = null ) {
300 // Separates classes with a single space, collates classes for post DIV
301 echo 'class="' . join( ' ', get_post_class( $class, $post_id ) ) . '"';
305 * Retrieve the classes for the post div as an array.
307 * The class names are add are many. If the post is a sticky, then the 'sticky'
308 * class name. The class 'hentry' is always added to each post. For each
309 * category, the class will be added with 'category-' with category slug is
310 * added. The tags are the same way as the categories with 'tag-' before the tag
311 * slug. All classes are passed through the filter, 'post_class' with the list
312 * of classes, followed by $class parameter value, with the post ID as the last
317 * @param string|array $class One or more classes to add to the class list.
318 * @param int $post_id An optional post ID.
319 * @return array Array of classes.
321 function get_post_class( $class = '', $post_id = null ) {
322 $post = get_post($post_id);
329 $classes[] = 'post-' . $post->ID;
331 $classes[] = $post->post_type;
332 $classes[] = 'type-' . $post->post_type;
333 $classes[] = 'status-' . $post->post_status;
336 if ( post_type_supports( $post->post_type, 'post-formats' ) ) {
337 $post_format = get_post_format( $post->ID );
339 if ( $post_format && !is_wp_error($post_format) )
340 $classes[] = 'format-' . sanitize_html_class( $post_format );
342 $classes[] = 'format-standard';
345 // post requires password
346 if ( post_password_required($post->ID) )
347 $classes[] = 'post-password-required';
349 // sticky for Sticky Posts
350 if ( is_sticky($post->ID) && is_home() && !is_paged() )
351 $classes[] = 'sticky';
353 // hentry for hAtom compliance
354 $classes[] = 'hentry';
357 if ( is_object_in_taxonomy( $post->post_type, 'category' ) ) {
358 foreach ( (array) get_the_category($post->ID) as $cat ) {
359 if ( empty($cat->slug ) )
361 $classes[] = 'category-' . sanitize_html_class($cat->slug, $cat->term_id);
366 if ( is_object_in_taxonomy( $post->post_type, 'post_tag' ) ) {
367 foreach ( (array) get_the_tags($post->ID) as $tag ) {
368 if ( empty($tag->slug ) )
370 $classes[] = 'tag-' . sanitize_html_class($tag->slug, $tag->term_id);
374 if ( !empty($class) ) {
375 if ( !is_array( $class ) )
376 $class = preg_split('#\s+#', $class);
377 $classes = array_merge($classes, $class);
380 $classes = array_map('esc_attr', $classes);
382 return apply_filters('post_class', $classes, $class, $post->ID);
386 * Display the classes for the body element.
390 * @param string|array $class One or more classes to add to the class list.
392 function body_class( $class = '' ) {
393 // Separates classes with a single space, collates classes for body element
394 echo 'class="' . join( ' ', get_body_class( $class ) ) . '"';
398 * Retrieve the classes for the body element as an array.
402 * @param string|array $class One or more classes to add to the class list.
403 * @return array Array of classes.
405 function get_body_class( $class = '' ) {
406 global $wp_query, $wpdb;
413 if ( is_front_page() )
418 $classes[] = 'archive';
422 $classes[] = 'search';
423 $classes[] = $wp_query->posts ? 'search-results' : 'search-no-results';
426 $classes[] = 'paged';
427 if ( is_attachment() )
428 $classes[] = 'attachment';
430 $classes[] = 'error404';
433 $post_id = $wp_query->get_queried_object_id();
434 $post = $wp_query->get_queried_object();
436 $classes[] = 'single';
437 if ( isset( $post->post_type ) ) {
438 $classes[] = 'single-' . sanitize_html_class($post->post_type, $post_id);
439 $classes[] = 'postid-' . $post_id;
442 if ( post_type_supports( $post->post_type, 'post-formats' ) ) {
443 $post_format = get_post_format( $post->ID );
445 if ( $post_format && !is_wp_error($post_format) )
446 $classes[] = 'single-format-' . sanitize_html_class( $post_format );
448 $classes[] = 'single-format-standard';
452 if ( is_attachment() ) {
453 $mime_type = get_post_mime_type($post_id);
454 $mime_prefix = array( 'application/', 'image/', 'text/', 'audio/', 'video/', 'music/' );
455 $classes[] = 'attachmentid-' . $post_id;
456 $classes[] = 'attachment-' . str_replace( $mime_prefix, '', $mime_type );
458 } elseif ( is_archive() ) {
459 if ( is_post_type_archive() ) {
460 $classes[] = 'post-type-archive';
461 $classes[] = 'post-type-archive-' . sanitize_html_class( get_query_var( 'post_type' ) );
462 } else if ( is_author() ) {
463 $author = $wp_query->get_queried_object();
464 $classes[] = 'author';
465 if ( isset( $author->user_nicename ) ) {
466 $classes[] = 'author-' . sanitize_html_class( $author->user_nicename, $author->ID );
467 $classes[] = 'author-' . $author->ID;
469 } elseif ( is_category() ) {
470 $cat = $wp_query->get_queried_object();
471 $classes[] = 'category';
472 if ( isset( $cat->term_id ) ) {
473 $classes[] = 'category-' . sanitize_html_class( $cat->slug, $cat->term_id );
474 $classes[] = 'category-' . $cat->term_id;
476 } elseif ( is_tag() ) {
477 $tags = $wp_query->get_queried_object();
479 if ( isset( $tags->term_id ) ) {
480 $classes[] = 'tag-' . sanitize_html_class( $tags->slug, $tags->term_id );
481 $classes[] = 'tag-' . $tags->term_id;
483 } elseif ( is_tax() ) {
484 $term = $wp_query->get_queried_object();
485 if ( isset( $term->term_id ) ) {
486 $classes[] = 'tax-' . sanitize_html_class( $term->taxonomy );
487 $classes[] = 'term-' . sanitize_html_class( $term->slug, $term->term_id );
488 $classes[] = 'term-' . $term->term_id;
491 } elseif ( is_page() ) {
494 $page_id = $wp_query->get_queried_object_id();
496 $post = get_post($page_id);
498 $classes[] = 'page-id-' . $page_id;
500 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) ) )
501 $classes[] = 'page-parent';
503 if ( $post->post_parent ) {
504 $classes[] = 'page-child';
505 $classes[] = 'parent-pageid-' . $post->post_parent;
507 if ( is_page_template() ) {
508 $classes[] = 'page-template';
509 $classes[] = 'page-template-' . sanitize_html_class( str_replace( '.', '-', get_page_template_slug( $page_id ) ) );
511 $classes[] = 'page-template-default';
515 if ( is_user_logged_in() )
516 $classes[] = 'logged-in';
518 if ( is_admin_bar_showing() ) {
519 $classes[] = 'admin-bar';
520 $classes[] = 'no-customize-support';
523 if ( get_theme_mod( 'background_color' ) || get_background_image() )
524 $classes[] = 'custom-background';
526 $page = $wp_query->get( 'page' );
528 if ( !$page || $page < 2)
529 $page = $wp_query->get( 'paged' );
531 if ( $page && $page > 1 ) {
532 $classes[] = 'paged-' . $page;
535 $classes[] = 'single-paged-' . $page;
537 $classes[] = 'page-paged-' . $page;
538 elseif ( is_category() )
539 $classes[] = 'category-paged-' . $page;
541 $classes[] = 'tag-paged-' . $page;
543 $classes[] = 'date-paged-' . $page;
544 elseif ( is_author() )
545 $classes[] = 'author-paged-' . $page;
546 elseif ( is_search() )
547 $classes[] = 'search-paged-' . $page;
548 elseif ( is_post_type_archive() )
549 $classes[] = 'post-type-paged-' . $page;
552 if ( ! empty( $class ) ) {
553 if ( !is_array( $class ) )
554 $class = preg_split( '#\s+#', $class );
555 $classes = array_merge( $classes, $class );
557 // Ensure that we always coerce class to being an array.
561 $classes = array_map( 'esc_attr', $classes );
563 return apply_filters( 'body_class', $classes, $class );
567 * Whether post requires password and correct password has been provided.
571 * @param int|WP_Post $post An optional post. Global $post used if not provided.
572 * @return bool false if a password is not required or the correct password cookie is present, true otherwise.
574 function post_password_required( $post = null ) {
575 $post = get_post($post);
577 if ( empty( $post->post_password ) )
580 if ( ! isset( $_COOKIE['wp-postpass_' . COOKIEHASH] ) )
583 require_once ABSPATH . 'wp-includes/class-phpass.php';
584 $hasher = new PasswordHash( 8, true );
586 $hash = wp_unslash( $_COOKIE[ 'wp-postpass_' . COOKIEHASH ] );
587 if ( 0 !== strpos( $hash, '$P$B' ) )
590 return ! $hasher->CheckPassword( $post->post_password, $hash );
594 * Page Template Functions for usage in Themes
597 * @subpackage Template
601 * The formatted output of a list of pages.
603 * Displays page links for paginated posts (i.e. includes the <!--nextpage-->.
604 * Quicktag one or more times). This tag must be within The Loop.
606 * The defaults for overwriting are:
607 * 'before' - Default is '<p> Pages:' (string). The html or text to prepend to
609 * 'after' - Default is '</p>' (string). The html or text to append to each
611 * 'link_before' - Default is '' (string). The html or text to prepend to each
612 * Pages link inside the <a> tag. Also prepended to the current item, which
614 * 'link_after' - Default is '' (string). The html or text to append to each
615 * Pages link inside the <a> tag. Also appended to the current item, which
617 * 'next_or_number' - Default is 'number' (string). Indicates whether page
618 * numbers should be used. Valid values are number and next.
619 * 'separator' - Default is ' ' (string). Text used between pagination links.
620 * 'nextpagelink' - Default is 'Next Page' (string). Text for link to next page.
622 * 'previouspagelink' - Default is 'Previous Page' (string). Text for link to
623 * previous page, if available.
624 * 'pagelink' - Default is '%' (String).Format string for page numbers. The % in
625 * the parameter string will be replaced with the page number, so Page %
626 * generates "Page 1", "Page 2", etc. Defaults to %, just the page number.
627 * 'echo' - Default is 1 (integer). When not 0, this triggers the HTML to be
628 * echoed and then returned.
632 * @param string|array $args Optional. Overwrite the defaults.
633 * @return string Formatted output in HTML.
635 function wp_link_pages( $args = '' ) {
637 'before' => '<p>' . __( 'Pages:' ),
641 'next_or_number' => 'number',
643 'nextpagelink' => __( 'Next page' ),
644 'previouspagelink' => __( 'Previous page' ),
649 $r = wp_parse_args( $args, $defaults );
650 $r = apply_filters( 'wp_link_pages_args', $r );
651 extract( $r, EXTR_SKIP );
653 global $page, $numpages, $multipage, $more, $pagenow;
657 if ( 'number' == $next_or_number ) {
659 for ( $i = 1; $i <= $numpages; $i++ ) {
660 $link = $link_before . str_replace( '%', $i, $pagelink ) . $link_after;
661 if ( $i != $page || ! $more && 1 == $page )
662 $link = _wp_link_page( $i ) . $link . '</a>';
663 $link = apply_filters( 'wp_link_pages_link', $link, $i );
664 $output .= $separator . $link;
671 $link = _wp_link_page( $i ) . $link_before . $previouspagelink . $link_after . '</a>';
672 $link = apply_filters( 'wp_link_pages_link', $link, $i );
673 $output .= $separator . $link;
676 if ( $i <= $numpages ) {
677 $link = _wp_link_page( $i ) . $link_before . $nextpagelink . $link_after . '</a>';
678 $link = apply_filters( 'wp_link_pages_link', $link, $i );
679 $output .= $separator . $link;
685 $output = apply_filters( 'wp_link_pages', $output, $args );
694 * Helper function for wp_link_pages().
699 * @param int $i Page number.
700 * @return string Link.
702 function _wp_link_page( $i ) {
707 $url = get_permalink();
709 if ( '' == get_option('permalink_structure') || in_array($post->post_status, array('draft', 'pending')) )
710 $url = add_query_arg( 'page', $i, get_permalink() );
711 elseif ( 'page' == get_option('show_on_front') && get_option('page_on_front') == $post->ID )
712 $url = trailingslashit(get_permalink()) . user_trailingslashit("$wp_rewrite->pagination_base/" . $i, 'single_paged');
714 $url = trailingslashit(get_permalink()) . user_trailingslashit($i, 'single_paged');
717 return '<a href="' . esc_url( $url ) . '">';
721 // Post-meta: Custom per-post fields.
725 * Retrieve post custom meta data field.
729 * @param string $key Meta data key name.
730 * @return bool|string|array Array of values or single value, if only one element exists. False will be returned if key does not exist.
732 function post_custom( $key = '' ) {
733 $custom = get_post_custom();
735 if ( !isset( $custom[$key] ) )
737 elseif ( 1 == count($custom[$key]) )
738 return $custom[$key][0];
740 return $custom[$key];
744 * Display list of post custom fields.
746 * @internal This will probably change at some point...
748 * @uses apply_filters() Calls 'the_meta_key' on list item HTML content, with key and value as separate parameters.
750 function the_meta() {
751 if ( $keys = get_post_custom_keys() ) {
752 echo "<ul class='post-meta'>\n";
753 foreach ( (array) $keys as $key ) {
755 if ( is_protected_meta( $keyt, 'post' ) )
757 $values = array_map('trim', get_post_custom_values($key));
758 $value = implode($values,', ');
759 echo apply_filters('the_meta_key', "<li><span class='post-meta-key'>$key:</span> $value</li>\n", $key, $value);
770 * Retrieve or display list of pages as a dropdown (select list).
774 * @param array|string $args Optional. Override default arguments.
775 * @return string HTML content, if not displaying.
777 function wp_dropdown_pages($args = '') {
779 'depth' => 0, 'child_of' => 0,
780 'selected' => 0, 'echo' => 1,
781 'name' => 'page_id', 'id' => '',
782 'show_option_none' => '', 'show_option_no_change' => '',
783 'option_none_value' => ''
786 $r = wp_parse_args( $args, $defaults );
787 extract( $r, EXTR_SKIP );
789 $pages = get_pages($r);
791 // Back-compat with old system where both id and name were based on $name argument
795 if ( ! empty($pages) ) {
796 $output = "<select name='" . esc_attr( $name ) . "' id='" . esc_attr( $id ) . "'>\n";
797 if ( $show_option_no_change )
798 $output .= "\t<option value=\"-1\">$show_option_no_change</option>";
799 if ( $show_option_none )
800 $output .= "\t<option value=\"" . esc_attr($option_none_value) . "\">$show_option_none</option>\n";
801 $output .= walk_page_dropdown_tree($pages, $depth, $r);
802 $output .= "</select>\n";
805 $output = apply_filters('wp_dropdown_pages', $output);
814 * Retrieve or display list of pages in list (li) format.
818 * @param array|string $args Optional. Override default arguments.
819 * @return string HTML content, if not displaying.
821 function wp_list_pages($args = '') {
823 'depth' => 0, 'show_date' => '',
824 'date_format' => get_option('date_format'),
825 'child_of' => 0, 'exclude' => '',
826 'title_li' => __('Pages'), 'echo' => 1,
827 'authors' => '', 'sort_column' => 'menu_order, post_title',
828 'link_before' => '', 'link_after' => '', 'walker' => '',
831 $r = wp_parse_args( $args, $defaults );
832 extract( $r, EXTR_SKIP );
837 // sanitize, mostly to keep spaces out
838 $r['exclude'] = preg_replace('/[^0-9,]/', '', $r['exclude']);
840 // Allow plugins to filter an array of excluded pages (but don't put a nullstring into the array)
841 $exclude_array = ( $r['exclude'] ) ? explode(',', $r['exclude']) : array();
842 $r['exclude'] = implode( ',', apply_filters('wp_list_pages_excludes', $exclude_array) );
845 $r['hierarchical'] = 0;
846 $pages = get_pages($r);
848 if ( !empty($pages) ) {
849 if ( $r['title_li'] )
850 $output .= '<li class="pagenav">' . $r['title_li'] . '<ul>';
853 if ( is_page() || is_attachment() || $wp_query->is_posts_page )
854 $current_page = $wp_query->get_queried_object_id();
855 $output .= walk_page_tree($pages, $r['depth'], $current_page, $r);
857 if ( $r['title_li'] )
858 $output .= '</ul></li>';
861 $output = apply_filters('wp_list_pages', $output, $r);
870 * Display or retrieve list of pages with optional home link.
872 * The arguments are listed below and part of the arguments are for {@link
873 * wp_list_pages()} function. Check that function for more info on those
877 * <li><strong>sort_column</strong> - How to sort the list of pages. Defaults
878 * to page title. Use column for posts table.</li>
879 * <li><strong>menu_class</strong> - Class to use for the div ID which contains
880 * the page list. Defaults to 'menu'.</li>
881 * <li><strong>echo</strong> - Whether to echo list or return it. Defaults to
883 * <li><strong>link_before</strong> - Text before show_home argument text.</li>
884 * <li><strong>link_after</strong> - Text after show_home argument text.</li>
885 * <li><strong>show_home</strong> - If you set this argument, then it will
886 * display the link to the home page. The show_home argument really just needs
887 * to be set to the value of the text of the link.</li>
892 * @param array|string $args
893 * @return string html menu
895 function wp_page_menu( $args = array() ) {
896 $defaults = array('sort_column' => 'menu_order, post_title', 'menu_class' => 'menu', 'echo' => true, 'link_before' => '', 'link_after' => '');
897 $args = wp_parse_args( $args, $defaults );
898 $args = apply_filters( 'wp_page_menu_args', $args );
904 // Show Home in the menu
905 if ( ! empty($args['show_home']) ) {
906 if ( true === $args['show_home'] || '1' === $args['show_home'] || 1 === $args['show_home'] )
909 $text = $args['show_home'];
911 if ( is_front_page() && !is_paged() )
912 $class = 'class="current_page_item"';
913 $menu .= '<li ' . $class . '><a href="' . home_url( '/' ) . '" title="' . esc_attr($text) . '">' . $args['link_before'] . $text . $args['link_after'] . '</a></li>';
914 // If the front page is a page, add it to the exclude list
915 if (get_option('show_on_front') == 'page') {
916 if ( !empty( $list_args['exclude'] ) ) {
917 $list_args['exclude'] .= ',';
919 $list_args['exclude'] = '';
921 $list_args['exclude'] .= get_option('page_on_front');
925 $list_args['echo'] = false;
926 $list_args['title_li'] = '';
927 $menu .= str_replace( array( "\r", "\n", "\t" ), '', wp_list_pages($list_args) );
930 $menu = '<ul>' . $menu . '</ul>';
932 $menu = '<div class="' . esc_attr($args['menu_class']) . '">' . $menu . "</div>\n";
933 $menu = apply_filters( 'wp_page_menu', $menu, $args );
945 * Retrieve HTML list content for page list.
947 * @uses Walker_Page to create HTML list content.
949 * @see Walker_Page::walk() for parameters and return description.
951 function walk_page_tree($pages, $depth, $current_page, $r) {
952 if ( empty($r['walker']) )
953 $walker = new Walker_Page;
955 $walker = $r['walker'];
957 $args = array($pages, $depth, $r, $current_page);
958 return call_user_func_array(array($walker, 'walk'), $args);
962 * Retrieve HTML dropdown (select) content for page list.
964 * @uses Walker_PageDropdown to create HTML dropdown content.
966 * @see Walker_PageDropdown::walk() for parameters and return description.
968 function walk_page_dropdown_tree() {
969 $args = func_get_args();
970 if ( empty($args[2]['walker']) ) // the user's options are the third parameter
971 $walker = new Walker_PageDropdown;
973 $walker = $args[2]['walker'];
975 return call_user_func_array(array($walker, 'walk'), $args);
979 * Create HTML list of pages.
985 class Walker_Page extends Walker {
987 * @see Walker::$tree_type
991 var $tree_type = 'page';
994 * @see Walker::$db_fields
996 * @todo Decouple this.
999 var $db_fields = array ('parent' => 'post_parent', 'id' => 'ID');
1002 * @see Walker::start_lvl()
1005 * @param string $output Passed by reference. Used to append additional content.
1006 * @param int $depth Depth of page. Used for padding.
1007 * @param array $args
1009 function start_lvl( &$output, $depth = 0, $args = array() ) {
1010 $indent = str_repeat("\t", $depth);
1011 $output .= "\n$indent<ul class='children'>\n";
1015 * @see Walker::end_lvl()
1018 * @param string $output Passed by reference. Used to append additional content.
1019 * @param int $depth Depth of page. Used for padding.
1020 * @param array $args
1022 function end_lvl( &$output, $depth = 0, $args = array() ) {
1023 $indent = str_repeat("\t", $depth);
1024 $output .= "$indent</ul>\n";
1028 * @see Walker::start_el()
1031 * @param string $output Passed by reference. Used to append additional content.
1032 * @param object $page Page data object.
1033 * @param int $depth Depth of page. Used for padding.
1034 * @param int $current_page Page ID.
1035 * @param array $args
1037 function start_el( &$output, $page, $depth = 0, $args = array(), $current_page = 0 ) {
1039 $indent = str_repeat("\t", $depth);
1043 extract($args, EXTR_SKIP);
1044 $css_class = array('page_item', 'page-item-'.$page->ID);
1045 if ( !empty($current_page) ) {
1046 $_current_page = get_post( $current_page );
1047 if ( in_array( $page->ID, $_current_page->ancestors ) )
1048 $css_class[] = 'current_page_ancestor';
1049 if ( $page->ID == $current_page )
1050 $css_class[] = 'current_page_item';
1051 elseif ( $_current_page && $page->ID == $_current_page->post_parent )
1052 $css_class[] = 'current_page_parent';
1053 } elseif ( $page->ID == get_option('page_for_posts') ) {
1054 $css_class[] = 'current_page_parent';
1057 $css_class = implode( ' ', apply_filters( 'page_css_class', $css_class, $page, $depth, $args, $current_page ) );
1059 if ( '' === $page->post_title )
1060 $page->post_title = sprintf( __( '#%d (no title)' ), $page->ID );
1062 $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>';
1064 if ( !empty($show_date) ) {
1065 if ( 'modified' == $show_date )
1066 $time = $page->post_modified;
1068 $time = $page->post_date;
1070 $output .= " " . mysql2date($date_format, $time);
1075 * @see Walker::end_el()
1078 * @param string $output Passed by reference. Used to append additional content.
1079 * @param object $page Page data object. Not used.
1080 * @param int $depth Depth of page. Not Used.
1081 * @param array $args
1083 function end_el( &$output, $page, $depth = 0, $args = array() ) {
1084 $output .= "</li>\n";
1090 * Create HTML dropdown list of pages.
1092 * @package WordPress
1096 class Walker_PageDropdown extends Walker {
1098 * @see Walker::$tree_type
1102 var $tree_type = 'page';
1105 * @see Walker::$db_fields
1107 * @todo Decouple this
1110 var $db_fields = array ('parent' => 'post_parent', 'id' => 'ID');
1113 * @see Walker::start_el()
1116 * @param string $output Passed by reference. Used to append additional content.
1117 * @param object $page Page data object.
1118 * @param int $depth Depth of page in reference to parent pages. Used for padding.
1119 * @param array $args Uses 'selected' argument for selected page to set selected HTML attribute for option element.
1122 function start_el( &$output, $page, $depth = 0, $args = array(), $id = 0 ) {
1123 $pad = str_repeat(' ', $depth * 3);
1125 $output .= "\t<option class=\"level-$depth\" value=\"$page->ID\"";
1126 if ( $page->ID == $args['selected'] )
1127 $output .= ' selected="selected"';
1129 $title = apply_filters( 'list_pages', $page->post_title, $page );
1130 $output .= $pad . esc_html( $title );
1131 $output .= "</option>\n";
1140 * Display an attachment page link using an image or icon.
1144 * @param int $id Optional. Post ID.
1145 * @param bool $fullsize Optional, default is false. Whether to use full size.
1146 * @param bool $deprecated Deprecated. Not used.
1147 * @param bool $permalink Optional, default is false. Whether to include permalink.
1149 function the_attachment_link( $id = 0, $fullsize = false, $deprecated = false, $permalink = false ) {
1150 if ( !empty( $deprecated ) )
1151 _deprecated_argument( __FUNCTION__, '2.5' );
1154 echo wp_get_attachment_link($id, 'full', $permalink);
1156 echo wp_get_attachment_link($id, 'thumbnail', $permalink);
1160 * Retrieve an attachment page link using an image or icon, if possible.
1163 * @uses apply_filters() Calls 'wp_get_attachment_link' filter on HTML content with same parameters as function.
1165 * @param int $id Optional. Post ID.
1166 * @param string $size Optional, default is 'thumbnail'. Size of image, either array or string.
1167 * @param bool $permalink Optional, default is false. Whether to add permalink to image.
1168 * @param bool $icon Optional, default is false. Whether to include icon.
1169 * @param string|bool $text Optional, default is false. If string, then will be link text.
1170 * @return string HTML content.
1172 function wp_get_attachment_link( $id = 0, $size = 'thumbnail', $permalink = false, $icon = false, $text = false ) {
1173 $id = intval( $id );
1174 $_post = get_post( $id );
1176 if ( empty( $_post ) || ( 'attachment' != $_post->post_type ) || ! $url = wp_get_attachment_url( $_post->ID ) )
1177 return __( 'Missing Attachment' );
1180 $url = get_attachment_link( $_post->ID );
1182 $post_title = esc_attr( $_post->post_title );
1186 elseif ( $size && 'none' != $size )
1187 $link_text = wp_get_attachment_image( $id, $size, $icon );
1191 if ( trim( $link_text ) == '' )
1192 $link_text = $_post->post_title;
1194 return apply_filters( 'wp_get_attachment_link', "<a href='$url' title='$post_title'>$link_text</a>", $id, $size, $permalink, $icon, $text );
1198 * Wrap attachment in <<p>> element before content.
1201 * @uses apply_filters() Calls 'prepend_attachment' hook on HTML content.
1203 * @param string $content
1206 function prepend_attachment($content) {
1209 if ( empty($post->post_type) || $post->post_type != 'attachment' )
1212 $p = '<p class="attachment">';
1213 // show the medium sized image representation of the attachment if available, and link to the raw file
1214 $p .= wp_get_attachment_link(0, 'medium', false);
1216 $p = apply_filters('prepend_attachment', $p);
1218 return "$p\n$content";
1226 * Retrieve protected post password form content.
1229 * @uses apply_filters() Calls 'the_password_form' filter on output.
1230 * @param int|WP_Post $post Optional. A post id or post object. Defaults to the current post when in The Loop, undefined otherwise.
1231 * @return string HTML content for password form for password protected post.
1233 function get_the_password_form( $post = 0 ) {
1234 $post = get_post( $post );
1235 $label = 'pwbox-' . ( empty($post->ID) ? rand() : $post->ID );
1236 $output = '<form action="' . esc_url( site_url( 'wp-login.php?action=postpass', 'login_post' ) ) . '" method="post">
1237 <p>' . __("This post is password protected. To view it please enter your password below:") . '</p>
1238 <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>
1241 return apply_filters('the_password_form', $output);
1245 * Whether currently in a page template.
1247 * This template tag allows you to determine if you are in a page template.
1248 * You can optionally provide a template name and then the check will be
1249 * specific to that template.
1254 * @param string $template The specific template name if specific matching is required.
1255 * @return bool True on success, false on failure.
1257 function is_page_template( $template = '' ) {
1261 $page_template = get_page_template_slug( get_queried_object_id() );
1263 if ( empty( $template ) )
1264 return (bool) $page_template;
1266 if ( $template == $page_template )
1269 if ( 'default' == $template && ! $page_template )
1276 * Get the specific template name for a page.
1280 * @param int $post_id Optional. The page ID to check. Defaults to the current post, when used in the loop.
1281 * @return string|bool Page template filename. Returns an empty string when the default page template
1282 * is in use. Returns false if the post is not a page.
1284 function get_page_template_slug( $post_id = null ) {
1285 $post = get_post( $post_id );
1286 if ( ! $post || 'page' != $post->post_type )
1288 $template = get_post_meta( $post->ID, '_wp_page_template', true );
1289 if ( ! $template || 'default' == $template )
1295 * Retrieve formatted date timestamp of a revision (linked to that revisions's page).
1297 * @package WordPress
1298 * @subpackage Post_Revisions
1303 * @param int|object $revision Revision ID or revision object.
1304 * @param bool $link Optional, default is true. Link to revisions's page?
1305 * @return string i18n formatted datetimestamp or localized 'Current Revision'.
1307 function wp_post_revision_title( $revision, $link = true ) {
1308 if ( !$revision = get_post( $revision ) )
1311 if ( !in_array( $revision->post_type, array( 'post', 'page', 'revision' ) ) )
1314 /* translators: revision date format, see http://php.net/date */
1315 $datef = _x( 'j F, Y @ G:i', 'revision date format');
1316 /* translators: 1: date */
1317 $autosavef = _x( '%1$s [Autosave]', 'post revision title extra' );
1318 /* translators: 1: date */
1319 $currentf = _x( '%1$s [Current Revision]', 'post revision title extra' );
1321 $date = date_i18n( $datef, strtotime( $revision->post_modified ) );
1322 if ( $link && current_user_can( 'edit_post', $revision->ID ) && $link = get_edit_post_link( $revision->ID ) )
1323 $date = "<a href='$link'>$date</a>";
1325 if ( !wp_is_post_revision( $revision ) )
1326 $date = sprintf( $currentf, $date );
1327 elseif ( wp_is_post_autosave( $revision ) )
1328 $date = sprintf( $autosavef, $date );
1334 * Retrieve formatted date timestamp of a revision (linked to that revisions's page).
1336 * @package WordPress
1337 * @subpackage Post_Revisions
1342 * @param int|object $revision Revision ID or revision object.
1343 * @param bool $link Optional, default is true. Link to revisions's page?
1344 * @return string gravatar, user, i18n formatted datetimestamp or localized 'Current Revision'.
1346 function wp_post_revision_title_expanded( $revision, $link = true ) {
1347 if ( !$revision = get_post( $revision ) )
1350 if ( !in_array( $revision->post_type, array( 'post', 'page', 'revision' ) ) )
1353 $author = get_the_author_meta( 'display_name', $revision->post_author );
1354 /* translators: revision date format, see http://php.net/date */
1355 $datef = _x( 'j F, Y @ G:i:s', 'revision date format');
1357 $gravatar = get_avatar( $revision->post_author, 24 );
1359 $date = date_i18n( $datef, strtotime( $revision->post_modified ) );
1360 if ( $link && current_user_can( 'edit_post', $revision->ID ) && $link = get_edit_post_link( $revision->ID ) )
1361 $date = "<a href='$link'>$date</a>";
1363 $revision_date_author = sprintf(
1364 /* translators: post revision title: 1: author avatar, 2: author name, 3: time ago, 4: date */
1365 _x( '%1$s %2$s, %3$s ago (%4$s)', 'post revision title' ),
1368 human_time_diff( strtotime( $revision->post_modified ), current_time( 'timestamp' ) ),
1372 $autosavef = __( '%1$s [Autosave]' );
1373 $currentf = __( '%1$s [Current Revision]' );
1375 if ( !wp_is_post_revision( $revision ) )
1376 $revision_date_author = sprintf( $currentf, $revision_date_author );
1377 elseif ( wp_is_post_autosave( $revision ) )
1378 $revision_date_author = sprintf( $autosavef, $revision_date_author );
1380 return $revision_date_author;
1384 * Display list of a post's revisions.
1386 * Can output either a UL with edit links or a TABLE with diff interface, and
1387 * restore action links.
1389 * @package WordPress
1390 * @subpackage Post_Revisions
1393 * @uses wp_get_post_revisions()
1394 * @uses wp_post_revision_title_expanded()
1395 * @uses get_edit_post_link()
1396 * @uses get_the_author_meta()
1398 * @param int|object $post_id Post ID or post object.
1399 * @param string $type 'all' (default), 'revision' or 'autosave'
1402 function wp_list_post_revisions( $post_id = 0, $type = 'all' ) {
1403 if ( ! $post = get_post( $post_id ) )
1406 // $args array with (parent, format, right, left, type) deprecated since 3.6
1407 if ( is_array( $type ) ) {
1408 $type = ! empty( $type['type'] ) ? $type['type'] : $type;
1409 _deprecated_argument( __FUNCTION__, '3.6' );
1412 if ( ! $revisions = wp_get_post_revisions( $post->ID ) )
1416 foreach ( $revisions as $revision ) {
1417 if ( ! current_user_can( 'read_post', $revision->ID ) )
1420 $is_autosave = wp_is_post_autosave( $revision );
1421 if ( ( 'revision' === $type && $is_autosave ) || ( 'autosave' === $type && ! $is_autosave ) )
1424 $rows .= "\t<li>" . wp_post_revision_title_expanded( $revision ) . "</li>\n";
1427 echo "<div class='hide-if-js'><p>" . __( 'JavaScript must be enabled to use this feature.' ) . "</p></div>\n";
1429 echo "<ul class='post-revisions hide-if-no-js'>\n";