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|WP_Post $post Optional. Post ID or post 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 ) ) {
112 * Filter the text prepended to the post title for protected posts.
114 * The filter is only applied on the front end.
118 * @param string $prepend Text displayed before the post title.
119 * Default 'Protected: %s'.
121 $protected_title_format = apply_filters( 'protected_title_format', __( 'Protected: %s' ) );
122 $title = sprintf( $protected_title_format, $title );
123 } else if ( isset( $post->post_status ) && 'private' == $post->post_status ) {
126 * Filter the text prepended to the post title of private posts.
128 * The filter is only applied on the front end.
132 * @param string $prepend Text displayed before the post title.
133 * Default 'Private: %s'.
135 $private_title_format = apply_filters( 'private_title_format', __( 'Private: %s' ) );
136 $title = sprintf( $private_title_format, $title );
141 * Filter the post title.
145 * @param string $title The post title.
146 * @param int $id The post ID.
148 return apply_filters( 'the_title', $title, $id );
152 * Display the Post Global Unique Identifier (guid).
154 * The guid will appear to be a link, but should not be used as an link to the
155 * post. The reason you should not use it as a link, is because of moving the
156 * blog across domains.
158 * Url is escaped to make it xml safe
162 * @param int|WP_Post $id Optional. Post ID or post object.
164 function the_guid( $id = 0 ) {
165 echo esc_url( get_the_guid( $id ) );
169 * Retrieve the Post Global Unique Identifier (guid).
171 * The guid will appear to be a link, but should not be used as an link to the
172 * post. The reason you should not use it as a link, is because of moving the
173 * blog across domains.
177 * @param int|WP_Post $id Optional. Post ID or post object.
180 function get_the_guid( $id = 0 ) {
181 $post = get_post($id);
184 * Filter the Global Unique Identifier (guid) of the post.
188 * @param string $post_guid Global Unique Identifier (guid) of the post.
190 return apply_filters( 'get_the_guid', $post->guid );
194 * Display the post content.
198 * @param string $more_link_text Optional. Content for when there is more text.
199 * @param bool $strip_teaser Optional. Strip teaser content before the more text. Default is false.
201 function the_content( $more_link_text = null, $strip_teaser = false) {
202 $content = get_the_content( $more_link_text, $strip_teaser );
205 * Filter the post content.
209 * @param string $content Content of the current post.
211 $content = apply_filters( 'the_content', $content );
212 $content = str_replace( ']]>', ']]>', $content );
217 * Retrieve the post content.
221 * @param string $more_link_text Optional. Content for when there is more text.
222 * @param bool $stripteaser Optional. Strip teaser content before the more text. Default is false.
225 function get_the_content( $more_link_text = null, $strip_teaser = false ) {
226 global $page, $more, $preview, $pages, $multipage;
230 if ( null === $more_link_text )
231 $more_link_text = __( '(more…)' );
236 // If post password required and it doesn't match the cookie.
237 if ( post_password_required( $post ) )
238 return get_the_password_form( $post );
240 if ( $page > count( $pages ) ) // if the requested page doesn't exist
241 $page = count( $pages ); // give them the highest numbered page that DOES exist
243 $content = $pages[$page - 1];
244 if ( preg_match( '/<!--more(.*?)?-->/', $content, $matches ) ) {
245 $content = explode( $matches[0], $content, 2 );
246 if ( ! empty( $matches[1] ) && ! empty( $more_link_text ) )
247 $more_link_text = strip_tags( wp_kses_no_null( trim( $matches[1] ) ) );
251 $content = array( $content );
254 if ( false !== strpos( $post->post_content, '<!--noteaser-->' ) && ( ! $multipage || $page == 1 ) )
255 $strip_teaser = true;
257 $teaser = $content[0];
259 if ( $more && $strip_teaser && $has_teaser )
264 if ( count( $content ) > 1 ) {
266 $output .= '<span id="more-' . $post->ID . '"></span>' . $content[1];
268 if ( ! empty( $more_link_text ) )
271 * Filter the Read More link text.
275 * @param string $more_link_element Read More link element.
276 * @param string $more_link_text Read More text.
278 $output .= apply_filters( 'the_content_more_link', ' <a href="' . get_permalink() . "#more-{$post->ID}\" class=\"more-link\">$more_link_text</a>", $more_link_text );
279 $output = force_balance_tags( $output );
283 if ( $preview ) // preview fix for javascript bug with foreign languages
284 $output = preg_replace_callback( '/\%u([0-9A-F]{4})/', '_convert_urlencoded_to_entities', $output );
290 * Preview fix for javascript bug with foreign languages
294 * @param array $match Match array from preg_replace_callback
297 function _convert_urlencoded_to_entities( $match ) {
298 return '&#' . base_convert( $match[1], 16, 10 ) . ';';
302 * Display the post excerpt.
306 function the_excerpt() {
309 * Filter the displayed post excerpt.
313 * @see get_the_excerpt()
315 * @param string $post_excerpt The post excerpt.
317 echo apply_filters( 'the_excerpt', get_the_excerpt() );
321 * Retrieve the post excerpt.
325 * @param mixed $deprecated Not used.
328 function get_the_excerpt( $deprecated = '' ) {
329 if ( !empty( $deprecated ) )
330 _deprecated_argument( __FUNCTION__, '2.3' );
334 if ( post_password_required() ) {
335 return __( 'There is no excerpt because this is a protected post.' );
339 * Filter the retrieved post excerpt.
343 * @param string $post_excerpt The post excerpt.
345 return apply_filters( 'get_the_excerpt', $post->post_excerpt );
349 * Whether post has excerpt.
353 * @param int|WP_Post $id Optional. Post ID or post object.
356 function has_excerpt( $id = 0 ) {
357 $post = get_post( $id );
358 return ( !empty( $post->post_excerpt ) );
362 * Display the classes for the post div.
366 * @param string|array $class One or more classes to add to the class list.
367 * @param int|WP_Post $post_id Optional. Post ID or post object.
369 function post_class( $class = '', $post_id = null ) {
370 // Separates classes with a single space, collates classes for post DIV
371 echo 'class="' . join( ' ', get_post_class( $class, $post_id ) ) . '"';
375 * Retrieve the classes for the post div as an array.
377 * The class names are many. If the post is a sticky, then the 'sticky'
378 * class name. The class 'hentry' is always added to each post. If the post has a
379 * post thumbnail, 'has-post-thumbnail' is added as a class. For each
380 * category, the class will be added with 'category-' with category slug is
381 * added. The tags are the same way as the categories with 'tag-' before the tag
382 * slug. All classes are passed through the filter, 'post_class' with the list
383 * of classes, followed by $class parameter value, with the post ID as the last
388 * @param string|array $class One or more classes to add to the class list.
389 * @param int|WP_Post $post_id Optional. Post ID or post object.
390 * @return array Array of classes.
392 function get_post_class( $class = '', $post_id = null ) {
393 $post = get_post($post_id);
400 $classes[] = 'post-' . $post->ID;
402 $classes[] = $post->post_type;
403 $classes[] = 'type-' . $post->post_type;
404 $classes[] = 'status-' . $post->post_status;
407 if ( post_type_supports( $post->post_type, 'post-formats' ) ) {
408 $post_format = get_post_format( $post->ID );
410 if ( $post_format && !is_wp_error($post_format) )
411 $classes[] = 'format-' . sanitize_html_class( $post_format );
413 $classes[] = 'format-standard';
416 // Post requires password
417 if ( post_password_required( $post->ID ) ) {
418 $classes[] = 'post-password-required';
420 } elseif ( ! is_attachment( $post ) && current_theme_supports( 'post-thumbnails' ) && has_post_thumbnail( $post->ID ) ) {
421 $classes[] = 'has-post-thumbnail';
424 // sticky for Sticky Posts
425 if ( is_sticky($post->ID) && is_home() && !is_paged() )
426 $classes[] = 'sticky';
428 // hentry for hAtom compliance
429 $classes[] = 'hentry';
432 if ( is_object_in_taxonomy( $post->post_type, 'category' ) ) {
433 foreach ( (array) get_the_category($post->ID) as $cat ) {
434 if ( empty($cat->slug ) )
436 $classes[] = 'category-' . sanitize_html_class($cat->slug, $cat->term_id);
441 if ( is_object_in_taxonomy( $post->post_type, 'post_tag' ) ) {
442 foreach ( (array) get_the_tags($post->ID) as $tag ) {
443 if ( empty($tag->slug ) )
445 $classes[] = 'tag-' . sanitize_html_class($tag->slug, $tag->term_id);
449 if ( !empty($class) ) {
450 if ( !is_array( $class ) )
451 $class = preg_split('#\s+#', $class);
452 $classes = array_merge($classes, $class);
455 $classes = array_map('esc_attr', $classes);
458 * Filter the list of CSS classes for the current post.
462 * @param array $classes An array of post classes.
463 * @param string $class A comma-separated list of additional classes added to the post.
464 * @param int $post_id The post ID.
466 return apply_filters( 'post_class', $classes, $class, $post->ID );
470 * Display the classes for the body element.
474 * @param string|array $class One or more classes to add to the class list.
476 function body_class( $class = '' ) {
477 // Separates classes with a single space, collates classes for body element
478 echo 'class="' . join( ' ', get_body_class( $class ) ) . '"';
482 * Retrieve the classes for the body element as an array.
486 * @param string|array $class One or more classes to add to the class list.
487 * @return array Array of classes.
489 function get_body_class( $class = '' ) {
490 global $wp_query, $wpdb;
497 if ( is_front_page() )
502 $classes[] = 'archive';
506 $classes[] = 'search';
507 $classes[] = $wp_query->posts ? 'search-results' : 'search-no-results';
510 $classes[] = 'paged';
511 if ( is_attachment() )
512 $classes[] = 'attachment';
514 $classes[] = 'error404';
517 $post_id = $wp_query->get_queried_object_id();
518 $post = $wp_query->get_queried_object();
520 $classes[] = 'single';
521 if ( isset( $post->post_type ) ) {
522 $classes[] = 'single-' . sanitize_html_class($post->post_type, $post_id);
523 $classes[] = 'postid-' . $post_id;
526 if ( post_type_supports( $post->post_type, 'post-formats' ) ) {
527 $post_format = get_post_format( $post->ID );
529 if ( $post_format && !is_wp_error($post_format) )
530 $classes[] = 'single-format-' . sanitize_html_class( $post_format );
532 $classes[] = 'single-format-standard';
536 if ( is_attachment() ) {
537 $mime_type = get_post_mime_type($post_id);
538 $mime_prefix = array( 'application/', 'image/', 'text/', 'audio/', 'video/', 'music/' );
539 $classes[] = 'attachmentid-' . $post_id;
540 $classes[] = 'attachment-' . str_replace( $mime_prefix, '', $mime_type );
542 } elseif ( is_archive() ) {
543 if ( is_post_type_archive() ) {
544 $classes[] = 'post-type-archive';
545 $post_type = get_query_var( 'post_type' );
546 if ( is_array( $post_type ) )
547 $post_type = reset( $post_type );
548 $classes[] = 'post-type-archive-' . sanitize_html_class( $post_type );
549 } else if ( is_author() ) {
550 $author = $wp_query->get_queried_object();
551 $classes[] = 'author';
552 if ( isset( $author->user_nicename ) ) {
553 $classes[] = 'author-' . sanitize_html_class( $author->user_nicename, $author->ID );
554 $classes[] = 'author-' . $author->ID;
556 } elseif ( is_category() ) {
557 $cat = $wp_query->get_queried_object();
558 $classes[] = 'category';
559 if ( isset( $cat->term_id ) ) {
560 $classes[] = 'category-' . sanitize_html_class( $cat->slug, $cat->term_id );
561 $classes[] = 'category-' . $cat->term_id;
563 } elseif ( is_tag() ) {
564 $tags = $wp_query->get_queried_object();
566 if ( isset( $tags->term_id ) ) {
567 $classes[] = 'tag-' . sanitize_html_class( $tags->slug, $tags->term_id );
568 $classes[] = 'tag-' . $tags->term_id;
570 } elseif ( is_tax() ) {
571 $term = $wp_query->get_queried_object();
572 if ( isset( $term->term_id ) ) {
573 $classes[] = 'tax-' . sanitize_html_class( $term->taxonomy );
574 $classes[] = 'term-' . sanitize_html_class( $term->slug, $term->term_id );
575 $classes[] = 'term-' . $term->term_id;
578 } elseif ( is_page() ) {
581 $page_id = $wp_query->get_queried_object_id();
583 $post = get_post($page_id);
585 $classes[] = 'page-id-' . $page_id;
587 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) ) )
588 $classes[] = 'page-parent';
590 if ( $post->post_parent ) {
591 $classes[] = 'page-child';
592 $classes[] = 'parent-pageid-' . $post->post_parent;
594 if ( is_page_template() ) {
595 $classes[] = 'page-template';
596 $classes[] = 'page-template-' . sanitize_html_class( str_replace( '.', '-', get_page_template_slug( $page_id ) ) );
598 $classes[] = 'page-template-default';
602 if ( is_user_logged_in() )
603 $classes[] = 'logged-in';
605 if ( is_admin_bar_showing() ) {
606 $classes[] = 'admin-bar';
607 $classes[] = 'no-customize-support';
610 if ( get_theme_mod( 'background_color' ) || get_background_image() )
611 $classes[] = 'custom-background';
613 $page = $wp_query->get( 'page' );
615 if ( !$page || $page < 2)
616 $page = $wp_query->get( 'paged' );
618 if ( $page && $page > 1 ) {
619 $classes[] = 'paged-' . $page;
622 $classes[] = 'single-paged-' . $page;
624 $classes[] = 'page-paged-' . $page;
625 elseif ( is_category() )
626 $classes[] = 'category-paged-' . $page;
628 $classes[] = 'tag-paged-' . $page;
630 $classes[] = 'date-paged-' . $page;
631 elseif ( is_author() )
632 $classes[] = 'author-paged-' . $page;
633 elseif ( is_search() )
634 $classes[] = 'search-paged-' . $page;
635 elseif ( is_post_type_archive() )
636 $classes[] = 'post-type-paged-' . $page;
639 if ( ! empty( $class ) ) {
640 if ( !is_array( $class ) )
641 $class = preg_split( '#\s+#', $class );
642 $classes = array_merge( $classes, $class );
644 // Ensure that we always coerce class to being an array.
648 $classes = array_map( 'esc_attr', $classes );
651 * Filter the list of CSS body classes for the current post or page.
655 * @param array $classes An array of body classes.
656 * @param string $class A comma-separated list of additional classes added to the body.
658 return apply_filters( 'body_class', $classes, $class );
662 * Whether post requires password and correct password has been provided.
666 * @param int|WP_Post $post An optional post. Global $post used if not provided.
667 * @return bool false if a password is not required or the correct password cookie is present, true otherwise.
669 function post_password_required( $post = null ) {
670 $post = get_post($post);
672 if ( empty( $post->post_password ) )
675 if ( ! isset( $_COOKIE['wp-postpass_' . COOKIEHASH] ) )
678 require_once ABSPATH . 'wp-includes/class-phpass.php';
679 $hasher = new PasswordHash( 8, true );
681 $hash = wp_unslash( $_COOKIE[ 'wp-postpass_' . COOKIEHASH ] );
682 if ( 0 !== strpos( $hash, '$P$B' ) )
685 return ! $hasher->CheckPassword( $post->post_password, $hash );
689 // Page Template Functions for usage in Themes
693 * The formatted output of a list of pages.
695 * Displays page links for paginated posts (i.e. includes the <!--nextpage-->.
696 * Quicktag one or more times). This tag must be within The Loop.
698 * The defaults for overwriting are:
699 * 'before' - Default is '<p> Pages:' (string). The html or text to prepend to
701 * 'after' - Default is '</p>' (string). The html or text to append to each
703 * 'link_before' - Default is '' (string). The html or text to prepend to each
704 * Pages link inside the <a> tag. Also prepended to the current item, which
706 * 'link_after' - Default is '' (string). The html or text to append to each
707 * Pages link inside the <a> tag. Also appended to the current item, which
709 * 'next_or_number' - Default is 'number' (string). Indicates whether page
710 * numbers should be used. Valid values are number and next.
711 * 'separator' - Default is ' ' (string). Text used between pagination links.
712 * 'nextpagelink' - Default is 'Next Page' (string). Text for link to next page.
714 * 'previouspagelink' - Default is 'Previous Page' (string). Text for link to
715 * previous page, if available.
716 * 'pagelink' - Default is '%' (String).Format string for page numbers. The % in
717 * the parameter string will be replaced with the page number, so Page %
718 * generates "Page 1", "Page 2", etc. Defaults to %, just the page number.
719 * 'echo' - Default is 1 (integer). When not 0, this triggers the HTML to be
720 * echoed and then returned.
724 * @param string|array $args Optional. Overwrite the defaults.
725 * @return string Formatted output in HTML.
727 function wp_link_pages( $args = '' ) {
729 'before' => '<p>' . __( 'Pages:' ),
733 'next_or_number' => 'number',
735 'nextpagelink' => __( 'Next page' ),
736 'previouspagelink' => __( 'Previous page' ),
741 $r = wp_parse_args( $args, $defaults );
744 * Filter the arguments used in retrieving page links for paginated posts.
748 * @param array $r An array of arguments for page links for paginated posts.
750 $r = apply_filters( 'wp_link_pages_args', $r );
751 extract( $r, EXTR_SKIP );
753 global $page, $numpages, $multipage, $more;
757 if ( 'number' == $next_or_number ) {
759 for ( $i = 1; $i <= $numpages; $i++ ) {
760 $link = $link_before . str_replace( '%', $i, $pagelink ) . $link_after;
761 if ( $i != $page || ! $more && 1 == $page )
762 $link = _wp_link_page( $i ) . $link . '</a>';
765 * Filter the HTML output of individual page number links.
769 * @param string $link The page number HTML output.
770 * @param int $i Page number for paginated posts' page links.
772 $link = apply_filters( 'wp_link_pages_link', $link, $i );
773 $output .= $separator . $link;
780 $link = _wp_link_page( $i ) . $link_before . $previouspagelink . $link_after . '</a>';
782 /** This filter is documented in wp-includes/post-template.php */
783 $link = apply_filters( 'wp_link_pages_link', $link, $i );
784 $output .= $separator . $link;
787 if ( $i <= $numpages ) {
788 $link = _wp_link_page( $i ) . $link_before . $nextpagelink . $link_after . '</a>';
790 /** This filter is documented in wp-includes/post-template.php */
791 $link = apply_filters( 'wp_link_pages_link', $link, $i );
792 $output .= $separator . $link;
799 * Filter the HTML output of page links for paginated posts.
803 * @param string $output HTML output of paginated posts' page links.
804 * @param array $args An array of arguments.
806 $output = apply_filters( 'wp_link_pages', $output, $args );
815 * Helper function for wp_link_pages().
820 * @param int $i Page number.
821 * @return string Link.
823 function _wp_link_page( $i ) {
828 $url = get_permalink();
830 if ( '' == get_option('permalink_structure') || in_array($post->post_status, array('draft', 'pending')) )
831 $url = add_query_arg( 'page', $i, get_permalink() );
832 elseif ( 'page' == get_option('show_on_front') && get_option('page_on_front') == $post->ID )
833 $url = trailingslashit(get_permalink()) . user_trailingslashit("$wp_rewrite->pagination_base/" . $i, 'single_paged');
835 $url = trailingslashit(get_permalink()) . user_trailingslashit($i, 'single_paged');
838 if ( is_preview() ) {
839 $url = add_query_arg( array(
843 if ( ( 'draft' !== $post->post_status ) && isset( $_GET['preview_id'], $_GET['preview_nonce'] ) ) {
844 $url = add_query_arg( array(
845 'preview_id' => wp_unslash( $_GET['preview_id'] ),
846 'preview_nonce' => wp_unslash( $_GET['preview_nonce'] )
851 return '<a href="' . esc_url( $url ) . '">';
855 // Post-meta: Custom per-post fields.
859 * Retrieve post custom meta data field.
863 * @param string $key Meta data key name.
864 * @return bool|string|array Array of values or single value, if only one element exists. False will be returned if key does not exist.
866 function post_custom( $key = '' ) {
867 $custom = get_post_custom();
869 if ( !isset( $custom[$key] ) )
871 elseif ( 1 == count($custom[$key]) )
872 return $custom[$key][0];
874 return $custom[$key];
878 * Display list of post custom fields.
880 * @internal This will probably change at some point...
882 * @uses apply_filters() Calls 'the_meta_key' on list item HTML content, with key and value as separate parameters.
884 function the_meta() {
885 if ( $keys = get_post_custom_keys() ) {
886 echo "<ul class='post-meta'>\n";
887 foreach ( (array) $keys as $key ) {
889 if ( is_protected_meta( $keyt, 'post' ) )
891 $values = array_map('trim', get_post_custom_values($key));
892 $value = implode($values,', ');
895 * Filter the HTML output of the li element in the post custom fields list.
899 * @param string $html The HTML output for the li element.
900 * @param string $key Meta key.
901 * @param string $value Meta value.
903 echo apply_filters( 'the_meta_key', "<li><span class='post-meta-key'>$key:</span> $value</li>\n", $key, $value );
914 * Retrieve or display list of pages as a dropdown (select list).
918 * @param array|string $args Optional. Override default arguments.
919 * @return string HTML content, if not displaying.
921 function wp_dropdown_pages($args = '') {
923 'depth' => 0, 'child_of' => 0,
924 'selected' => 0, 'echo' => 1,
925 'name' => 'page_id', 'id' => '',
926 'show_option_none' => '', 'show_option_no_change' => '',
927 'option_none_value' => ''
930 $r = wp_parse_args( $args, $defaults );
931 extract( $r, EXTR_SKIP );
933 $pages = get_pages($r);
935 // Back-compat with old system where both id and name were based on $name argument
939 if ( ! empty($pages) ) {
940 $output = "<select name='" . esc_attr( $name ) . "' id='" . esc_attr( $id ) . "'>\n";
941 if ( $show_option_no_change )
942 $output .= "\t<option value=\"-1\">$show_option_no_change</option>";
943 if ( $show_option_none )
944 $output .= "\t<option value=\"" . esc_attr($option_none_value) . "\">$show_option_none</option>\n";
945 $output .= walk_page_dropdown_tree($pages, $depth, $r);
946 $output .= "</select>\n";
950 * Filter the HTML output of a list of pages as a drop down.
954 * @param string $output HTML output for drop down list of pages.
956 $output = apply_filters( 'wp_dropdown_pages', $output );
965 * Retrieve or display list of pages in list (li) format.
969 * @param array|string $args Optional. Override default arguments.
970 * @return string HTML content, if not displaying.
972 function wp_list_pages($args = '') {
974 'depth' => 0, 'show_date' => '',
975 'date_format' => get_option('date_format'),
976 'child_of' => 0, 'exclude' => '',
977 'title_li' => __('Pages'), 'echo' => 1,
978 'authors' => '', 'sort_column' => 'menu_order, post_title',
979 'link_before' => '', 'link_after' => '', 'walker' => '',
982 $r = wp_parse_args( $args, $defaults );
983 extract( $r, EXTR_SKIP );
988 // sanitize, mostly to keep spaces out
989 $r['exclude'] = preg_replace('/[^0-9,]/', '', $r['exclude']);
991 // Allow plugins to filter an array of excluded pages (but don't put a nullstring into the array)
992 $exclude_array = ( $r['exclude'] ) ? explode(',', $r['exclude']) : array();
995 * Filter the array of pages to exclude from the pages list.
999 * @param array $exclude_array An array of page IDs to exclude.
1001 $r['exclude'] = implode( ',', apply_filters( 'wp_list_pages_excludes', $exclude_array ) );
1004 $r['hierarchical'] = 0;
1005 $pages = get_pages($r);
1007 if ( !empty($pages) ) {
1008 if ( $r['title_li'] )
1009 $output .= '<li class="pagenav">' . $r['title_li'] . '<ul>';
1012 if ( is_page() || is_attachment() || $wp_query->is_posts_page ) {
1013 $current_page = get_queried_object_id();
1014 } elseif ( is_singular() ) {
1015 $queried_object = get_queried_object();
1016 if ( is_post_type_hierarchical( $queried_object->post_type ) ) {
1017 $current_page = $queried_object->ID;
1021 $output .= walk_page_tree($pages, $r['depth'], $current_page, $r);
1023 if ( $r['title_li'] )
1024 $output .= '</ul></li>';
1028 * Filter the HTML output of the pages to list.
1032 * @see wp_list_pages()
1034 * @param string $output HTML output of the pages list.
1035 * @param array $r An array of page-listing arguments.
1037 $output = apply_filters( 'wp_list_pages', $output, $r );
1046 * Display or retrieve list of pages with optional home link.
1048 * The arguments are listed below and part of the arguments are for {@link
1049 * wp_list_pages()} function. Check that function for more info on those
1053 * <li><strong>sort_column</strong> - How to sort the list of pages. Defaults
1054 * to 'menu_order, post_title'. Use column for posts table.</li>
1055 * <li><strong>menu_class</strong> - Class to use for the div ID which contains
1056 * the page list. Defaults to 'menu'.</li>
1057 * <li><strong>echo</strong> - Whether to echo list or return it. Defaults to
1059 * <li><strong>link_before</strong> - Text before show_home argument text.</li>
1060 * <li><strong>link_after</strong> - Text after show_home argument text.</li>
1061 * <li><strong>show_home</strong> - If you set this argument, then it will
1062 * display the link to the home page. The show_home argument really just needs
1063 * to be set to the value of the text of the link.</li>
1068 * @param array|string $args
1069 * @return string html menu
1071 function wp_page_menu( $args = array() ) {
1072 $defaults = array('sort_column' => 'menu_order, post_title', 'menu_class' => 'menu', 'echo' => true, 'link_before' => '', 'link_after' => '');
1073 $args = wp_parse_args( $args, $defaults );
1076 * Filter the arguments used to generate a page-based menu.
1080 * @see wp_page_menu()
1082 * @param array $args An array of page menu arguments.
1084 $args = apply_filters( 'wp_page_menu_args', $args );
1090 // Show Home in the menu
1091 if ( ! empty($args['show_home']) ) {
1092 if ( true === $args['show_home'] || '1' === $args['show_home'] || 1 === $args['show_home'] )
1095 $text = $args['show_home'];
1097 if ( is_front_page() && !is_paged() )
1098 $class = 'class="current_page_item"';
1099 $menu .= '<li ' . $class . '><a href="' . home_url( '/' ) . '">' . $args['link_before'] . $text . $args['link_after'] . '</a></li>';
1100 // If the front page is a page, add it to the exclude list
1101 if (get_option('show_on_front') == 'page') {
1102 if ( !empty( $list_args['exclude'] ) ) {
1103 $list_args['exclude'] .= ',';
1105 $list_args['exclude'] = '';
1107 $list_args['exclude'] .= get_option('page_on_front');
1111 $list_args['echo'] = false;
1112 $list_args['title_li'] = '';
1113 $menu .= str_replace( array( "\r", "\n", "\t" ), '', wp_list_pages($list_args) );
1116 $menu = '<ul>' . $menu . '</ul>';
1118 $menu = '<div class="' . esc_attr($args['menu_class']) . '">' . $menu . "</div>\n";
1121 * Filter the HTML output of a page-based menu.
1125 * @see wp_page_menu()
1127 * @param string $menu The HTML output.
1128 * @param array $args An array of arguments.
1130 $menu = apply_filters( 'wp_page_menu', $menu, $args );
1131 if ( $args['echo'] )
1142 * Retrieve HTML list content for page list.
1144 * @uses Walker_Page to create HTML list content.
1146 * @see Walker_Page::walk() for parameters and return description.
1148 function walk_page_tree($pages, $depth, $current_page, $r) {
1149 if ( empty($r['walker']) )
1150 $walker = new Walker_Page;
1152 $walker = $r['walker'];
1154 foreach ( (array) $pages as $page ) {
1155 if ( $page->post_parent )
1156 $r['pages_with_children'][ $page->post_parent ] = true;
1159 $args = array($pages, $depth, $r, $current_page);
1160 return call_user_func_array(array($walker, 'walk'), $args);
1164 * Retrieve HTML dropdown (select) content for page list.
1166 * @uses Walker_PageDropdown to create HTML dropdown content.
1168 * @see Walker_PageDropdown::walk() for parameters and return description.
1170 function walk_page_dropdown_tree() {
1171 $args = func_get_args();
1172 if ( empty($args[2]['walker']) ) // the user's options are the third parameter
1173 $walker = new Walker_PageDropdown;
1175 $walker = $args[2]['walker'];
1177 return call_user_func_array(array($walker, 'walk'), $args);
1181 * Create HTML list of pages.
1186 class Walker_Page extends Walker {
1188 * @see Walker::$tree_type
1192 var $tree_type = 'page';
1195 * @see Walker::$db_fields
1197 * @todo Decouple this.
1200 var $db_fields = array ('parent' => 'post_parent', 'id' => 'ID');
1203 * @see Walker::start_lvl()
1206 * @param string $output Passed by reference. Used to append additional content.
1207 * @param int $depth Depth of page. Used for padding.
1208 * @param array $args
1210 function start_lvl( &$output, $depth = 0, $args = array() ) {
1211 $indent = str_repeat("\t", $depth);
1212 $output .= "\n$indent<ul class='children'>\n";
1216 * @see Walker::end_lvl()
1219 * @param string $output Passed by reference. Used to append additional content.
1220 * @param int $depth Depth of page. Used for padding.
1221 * @param array $args
1223 function end_lvl( &$output, $depth = 0, $args = array() ) {
1224 $indent = str_repeat("\t", $depth);
1225 $output .= "$indent</ul>\n";
1229 * @see Walker::start_el()
1232 * @param string $output Passed by reference. Used to append additional content.
1233 * @param object $page Page data object.
1234 * @param int $depth Depth of page. Used for padding.
1235 * @param int $current_page Page ID.
1236 * @param array $args
1238 function start_el( &$output, $page, $depth = 0, $args = array(), $current_page = 0 ) {
1240 $indent = str_repeat("\t", $depth);
1244 extract($args, EXTR_SKIP);
1245 $css_class = array('page_item', 'page-item-'.$page->ID);
1247 if( isset( $args['pages_with_children'][ $page->ID ] ) )
1248 $css_class[] = 'page_item_has_children';
1250 if ( !empty($current_page) ) {
1251 $_current_page = get_post( $current_page );
1252 if ( in_array( $page->ID, $_current_page->ancestors ) )
1253 $css_class[] = 'current_page_ancestor';
1254 if ( $page->ID == $current_page )
1255 $css_class[] = 'current_page_item';
1256 elseif ( $_current_page && $page->ID == $_current_page->post_parent )
1257 $css_class[] = 'current_page_parent';
1258 } elseif ( $page->ID == get_option('page_for_posts') ) {
1259 $css_class[] = 'current_page_parent';
1263 * Filter the list of CSS classes to include with each page item in the list.
1267 * @see wp_list_pages()
1269 * @param array $css_class An array of CSS classes to be applied
1270 * to each list item.
1271 * @param WP_Post $page Page data object.
1272 * @param int $depth Depth of page, used for padding.
1273 * @param array $args An array of arguments.
1274 * @param int $current_page ID of the current page.
1276 $css_class = implode( ' ', apply_filters( 'page_css_class', $css_class, $page, $depth, $args, $current_page ) );
1278 if ( '' === $page->post_title )
1279 $page->post_title = sprintf( __( '#%d (no title)' ), $page->ID );
1281 /** This filter is documented in wp-includes/post-template.php */
1282 $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>';
1284 if ( !empty($show_date) ) {
1285 if ( 'modified' == $show_date )
1286 $time = $page->post_modified;
1288 $time = $page->post_date;
1290 $output .= " " . mysql2date($date_format, $time);
1295 * @see Walker::end_el()
1298 * @param string $output Passed by reference. Used to append additional content.
1299 * @param object $page Page data object. Not used.
1300 * @param int $depth Depth of page. Not Used.
1301 * @param array $args
1303 function end_el( &$output, $page, $depth = 0, $args = array() ) {
1304 $output .= "</li>\n";
1310 * Create HTML dropdown list of pages.
1315 class Walker_PageDropdown extends Walker {
1317 * @see Walker::$tree_type
1321 var $tree_type = 'page';
1324 * @see Walker::$db_fields
1326 * @todo Decouple this
1329 var $db_fields = array ('parent' => 'post_parent', 'id' => 'ID');
1332 * @see Walker::start_el()
1335 * @param string $output Passed by reference. Used to append additional content.
1336 * @param object $page Page data object.
1337 * @param int $depth Depth of page in reference to parent pages. Used for padding.
1338 * @param array $args Uses 'selected' argument for selected page to set selected HTML attribute for option element.
1341 function start_el( &$output, $page, $depth = 0, $args = array(), $id = 0 ) {
1342 $pad = str_repeat(' ', $depth * 3);
1344 $output .= "\t<option class=\"level-$depth\" value=\"$page->ID\"";
1345 if ( $page->ID == $args['selected'] )
1346 $output .= ' selected="selected"';
1349 $title = $page->post_title;
1350 if ( '' === $title ) {
1351 $title = sprintf( __( '#%d (no title)' ), $page->ID );
1355 * Filter the page title when creating an HTML drop-down list of pages.
1359 * @param string $title Page title.
1360 * @param object $page Page data object.
1362 $title = apply_filters( 'list_pages', $title, $page );
1363 $output .= $pad . esc_html( $title );
1364 $output .= "</option>\n";
1373 * Display an attachment page link using an image or icon.
1377 * @param int|WP_Post $id Optional. Post ID or post object.
1378 * @param bool $fullsize Optional, default is false. Whether to use full size.
1379 * @param bool $deprecated Deprecated. Not used.
1380 * @param bool $permalink Optional, default is false. Whether to include permalink.
1382 function the_attachment_link( $id = 0, $fullsize = false, $deprecated = false, $permalink = false ) {
1383 if ( !empty( $deprecated ) )
1384 _deprecated_argument( __FUNCTION__, '2.5' );
1387 echo wp_get_attachment_link($id, 'full', $permalink);
1389 echo wp_get_attachment_link($id, 'thumbnail', $permalink);
1393 * Retrieve an attachment page link using an image or icon, if possible.
1396 * @uses apply_filters() Calls 'wp_get_attachment_link' filter on HTML content with same parameters as function.
1398 * @param int|WP_Post $id Optional. Post ID or post object.
1399 * @param string $size Optional, default is 'thumbnail'. Size of image, either array or string.
1400 * @param bool $permalink Optional, default is false. Whether to add permalink to image.
1401 * @param bool $icon Optional, default is false. Whether to include icon.
1402 * @param string|bool $text Optional, default is false. If string, then will be link text.
1403 * @return string HTML content.
1405 function wp_get_attachment_link( $id = 0, $size = 'thumbnail', $permalink = false, $icon = false, $text = false ) {
1406 $id = intval( $id );
1407 $_post = get_post( $id );
1409 if ( empty( $_post ) || ( 'attachment' != $_post->post_type ) || ! $url = wp_get_attachment_url( $_post->ID ) )
1410 return __( 'Missing Attachment' );
1413 $url = get_attachment_link( $_post->ID );
1415 $post_title = esc_attr( $_post->post_title );
1419 elseif ( $size && 'none' != $size )
1420 $link_text = wp_get_attachment_image( $id, $size, $icon );
1424 if ( trim( $link_text ) == '' )
1425 $link_text = $_post->post_title;
1428 * Filter a retrieved attachment page link.
1432 * @param string $link_html The page link HTML output.
1433 * @param int $id Post ID.
1434 * @param string $size Image size. Default 'thumbnail'.
1435 * @param bool $permalink Whether to add permalink to image. Default false.
1436 * @param bool $icon Whether to include an icon. Default false.
1437 * @param string|bool $text If string, will be link text. Default false.
1439 return apply_filters( 'wp_get_attachment_link', "<a href='$url'>$link_text</a>", $id, $size, $permalink, $icon, $text );
1443 * Wrap attachment in <<p>> element before content.
1447 * @param string $content
1450 function prepend_attachment($content) {
1453 if ( empty($post->post_type) || $post->post_type != 'attachment' )
1456 if ( 0 === strpos( $post->post_mime_type, 'video' ) ) {
1457 $meta = wp_get_attachment_metadata( get_the_ID() );
1458 $atts = array( 'src' => wp_get_attachment_url() );
1459 if ( ! empty( $meta['width'] ) && ! empty( $meta['height'] ) ) {
1460 $atts['width'] = (int) $meta['width'];
1461 $atts['height'] = (int) $meta['height'];
1463 $p = wp_video_shortcode( $atts );
1464 } elseif ( 0 === strpos( $post->post_mime_type, 'audio' ) ) {
1465 $p = wp_audio_shortcode( array( 'src' => wp_get_attachment_url() ) );
1467 $p = '<p class="attachment">';
1468 // show the medium sized image representation of the attachment if available, and link to the raw file
1469 $p .= wp_get_attachment_link(0, 'medium', false);
1474 * Filter the attachment markup to be prepended to the post content.
1478 * @see prepend_attachment()
1480 * @param string $p The attachment HTML output.
1482 $p = apply_filters( 'prepend_attachment', $p );
1484 return "$p\n$content";
1492 * Retrieve protected post password form content.
1495 * @uses apply_filters() Calls 'the_password_form' filter on output.
1496 * @param int|WP_Post $post Optional. A post ID or post object.
1497 * @return string HTML content for password form for password protected post.
1499 function get_the_password_form( $post = 0 ) {
1500 $post = get_post( $post );
1501 $label = 'pwbox-' . ( empty($post->ID) ? rand() : $post->ID );
1502 $output = '<form action="' . esc_url( site_url( 'wp-login.php?action=postpass', 'login_post' ) ) . '" class="post-password-form" method="post">
1503 <p>' . __( 'This content is password protected. To view it please enter your password below:' ) . '</p>
1504 <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></form>
1508 * Filter the HTML output for the protected post password form.
1510 * If modifying the password field, please note that the core database schema
1511 * limits the password field to 20 characters regardless of the value of the
1512 * size attribute in the form input.
1516 * @param string $output The password form HTML output.
1518 return apply_filters( 'the_password_form', $output );
1522 * Whether currently in a page template.
1524 * This template tag allows you to determine if you are in a page template.
1525 * You can optionally provide a template name and then the check will be
1526 * specific to that template.
1531 * @param string $template The specific template name if specific matching is required.
1532 * @return bool True on success, false on failure.
1534 function is_page_template( $template = '' ) {
1538 $page_template = get_page_template_slug( get_queried_object_id() );
1540 if ( empty( $template ) )
1541 return (bool) $page_template;
1543 if ( $template == $page_template )
1546 if ( 'default' == $template && ! $page_template )
1553 * Get the specific template name for a page.
1557 * @param int $post_id Optional. The page ID to check. Defaults to the current post, when used in the loop.
1558 * @return string|bool Page template filename. Returns an empty string when the default page template
1559 * is in use. Returns false if the post is not a page.
1561 function get_page_template_slug( $post_id = null ) {
1562 $post = get_post( $post_id );
1563 if ( ! $post || 'page' != $post->post_type )
1565 $template = get_post_meta( $post->ID, '_wp_page_template', true );
1566 if ( ! $template || 'default' == $template )
1572 * Retrieve formatted date timestamp of a revision (linked to that revisions's page).
1578 * @param int|object $revision Revision ID or revision object.
1579 * @param bool $link Optional, default is true. Link to revisions's page?
1580 * @return string i18n formatted datetimestamp or localized 'Current Revision'.
1582 function wp_post_revision_title( $revision, $link = true ) {
1583 if ( !$revision = get_post( $revision ) )
1586 if ( !in_array( $revision->post_type, array( 'post', 'page', 'revision' ) ) )
1589 /* translators: revision date format, see http://php.net/date */
1590 $datef = _x( 'j F, Y @ G:i', 'revision date format');
1591 /* translators: 1: date */
1592 $autosavef = _x( '%1$s [Autosave]', 'post revision title extra' );
1593 /* translators: 1: date */
1594 $currentf = _x( '%1$s [Current Revision]', 'post revision title extra' );
1596 $date = date_i18n( $datef, strtotime( $revision->post_modified ) );
1597 if ( $link && current_user_can( 'edit_post', $revision->ID ) && $link = get_edit_post_link( $revision->ID ) )
1598 $date = "<a href='$link'>$date</a>";
1600 if ( !wp_is_post_revision( $revision ) )
1601 $date = sprintf( $currentf, $date );
1602 elseif ( wp_is_post_autosave( $revision ) )
1603 $date = sprintf( $autosavef, $date );
1609 * Retrieve formatted date timestamp of a revision (linked to that revisions's page).
1615 * @param int|object $revision Revision ID or revision object.
1616 * @param bool $link Optional, default is true. Link to revisions's page?
1617 * @return string gravatar, user, i18n formatted datetimestamp or localized 'Current Revision'.
1619 function wp_post_revision_title_expanded( $revision, $link = true ) {
1620 if ( !$revision = get_post( $revision ) )
1623 if ( !in_array( $revision->post_type, array( 'post', 'page', 'revision' ) ) )
1626 $author = get_the_author_meta( 'display_name', $revision->post_author );
1627 /* translators: revision date format, see http://php.net/date */
1628 $datef = _x( 'j F, Y @ G:i:s', 'revision date format');
1630 $gravatar = get_avatar( $revision->post_author, 24 );
1632 $date = date_i18n( $datef, strtotime( $revision->post_modified ) );
1633 if ( $link && current_user_can( 'edit_post', $revision->ID ) && $link = get_edit_post_link( $revision->ID ) )
1634 $date = "<a href='$link'>$date</a>";
1636 $revision_date_author = sprintf(
1637 /* translators: post revision title: 1: author avatar, 2: author name, 3: time ago, 4: date */
1638 _x( '%1$s %2$s, %3$s ago (%4$s)', 'post revision title' ),
1641 human_time_diff( strtotime( $revision->post_modified ), current_time( 'timestamp' ) ),
1645 $autosavef = __( '%1$s [Autosave]' );
1646 $currentf = __( '%1$s [Current Revision]' );
1648 if ( !wp_is_post_revision( $revision ) )
1649 $revision_date_author = sprintf( $currentf, $revision_date_author );
1650 elseif ( wp_is_post_autosave( $revision ) )
1651 $revision_date_author = sprintf( $autosavef, $revision_date_author );
1653 return $revision_date_author;
1657 * Display list of a post's revisions.
1659 * Can output either a UL with edit links or a TABLE with diff interface, and
1660 * restore action links.
1664 * @uses wp_get_post_revisions()
1665 * @uses wp_post_revision_title_expanded()
1666 * @uses get_edit_post_link()
1667 * @uses get_the_author_meta()
1669 * @param int|WP_Post $post_id Optional. Post ID or post object.
1670 * @param string $type 'all' (default), 'revision' or 'autosave'
1673 function wp_list_post_revisions( $post_id = 0, $type = 'all' ) {
1674 if ( ! $post = get_post( $post_id ) )
1677 // $args array with (parent, format, right, left, type) deprecated since 3.6
1678 if ( is_array( $type ) ) {
1679 $type = ! empty( $type['type'] ) ? $type['type'] : $type;
1680 _deprecated_argument( __FUNCTION__, '3.6' );
1683 if ( ! $revisions = wp_get_post_revisions( $post->ID ) )
1687 foreach ( $revisions as $revision ) {
1688 if ( ! current_user_can( 'read_post', $revision->ID ) )
1691 $is_autosave = wp_is_post_autosave( $revision );
1692 if ( ( 'revision' === $type && $is_autosave ) || ( 'autosave' === $type && ! $is_autosave ) )
1695 $rows .= "\t<li>" . wp_post_revision_title_expanded( $revision ) . "</li>\n";
1698 echo "<div class='hide-if-js'><p>" . __( 'JavaScript must be enabled to use this feature.' ) . "</p></div>\n";
1700 echo "<ul class='post-revisions hide-if-no-js'>\n";