[ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * WordPress Post Template Functions. 4 * 5 * Gets content for the current post in the loop. 6 * 7 * @package WordPress 8 * @subpackage Template 9 */ 10 11 /** 12 * Displays the ID of the current item in the WordPress Loop. 13 * 14 * @since 0.71 15 */ 16 function the_ID() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid 17 echo get_the_ID(); 18 } 19 20 /** 21 * Retrieves the ID of the current item in the WordPress Loop. 22 * 23 * @since 2.1.0 24 * 25 * @return int|false The ID of the current item in the WordPress Loop. False if $post is not set. 26 */ 27 function get_the_ID() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid 28 $post = get_post(); 29 return ! empty( $post ) ? $post->ID : false; 30 } 31 32 /** 33 * Displays or retrieves the current post title with optional markup. 34 * 35 * @since 0.71 36 * 37 * @param string $before Optional. Markup to prepend to the title. Default empty. 38 * @param string $after Optional. Markup to append to the title. Default empty. 39 * @param bool $display Optional. Whether to echo or return the title. Default true for echo. 40 * @return void|string Void if `$display` argument is true or the title is empty, 41 * current post title if `$display` is false. 42 */ 43 function the_title( $before = '', $after = '', $display = true ) { 44 $title = get_the_title(); 45 46 if ( strlen( $title ) === 0 ) { 47 return; 48 } 49 50 $title = $before . $title . $after; 51 52 if ( $display ) { 53 echo $title; 54 } else { 55 return $title; 56 } 57 } 58 59 /** 60 * Sanitizes the current title when retrieving or displaying. 61 * 62 * Works like the_title(), except the parameters can be in a string or 63 * an array. See the function for what can be override in the $args parameter. 64 * 65 * The title before it is displayed will have the tags stripped and esc_attr() 66 * before it is passed to the user or displayed. The default as with the_title(), 67 * is to display the title. 68 * 69 * @since 2.3.0 70 * 71 * @param string|array $args { 72 * Title attribute arguments. Optional. 73 * 74 * @type string $before Markup to prepend to the title. Default empty. 75 * @type string $after Markup to append to the title. Default empty. 76 * @type bool $echo Whether to echo or return the title. Default true for echo. 77 * @type WP_Post $post Current post object to retrieve the title for. 78 * } 79 * @return void|string Void if 'echo' argument is true, the title attribute if 'echo' is false. 80 */ 81 function the_title_attribute( $args = '' ) { 82 $defaults = array( 83 'before' => '', 84 'after' => '', 85 'echo' => true, 86 'post' => get_post(), 87 ); 88 $parsed_args = wp_parse_args( $args, $defaults ); 89 90 $title = get_the_title( $parsed_args['post'] ); 91 92 if ( strlen( $title ) === 0 ) { 93 return; 94 } 95 96 $title = $parsed_args['before'] . $title . $parsed_args['after']; 97 $title = esc_attr( strip_tags( $title ) ); 98 99 if ( $parsed_args['echo'] ) { 100 echo $title; 101 } else { 102 return $title; 103 } 104 } 105 106 /** 107 * Retrieves the post title. 108 * 109 * If the post is protected and the visitor is not an admin, then "Protected" 110 * will be inserted before the post title. If the post is private, then 111 * "Private" will be inserted before the post title. 112 * 113 * @since 0.71 114 * 115 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post. 116 * @return string 117 */ 118 function get_the_title( $post = 0 ) { 119 $post = get_post( $post ); 120 121 $post_title = isset( $post->post_title ) ? $post->post_title : ''; 122 $post_id = isset( $post->ID ) ? $post->ID : 0; 123 124 if ( ! is_admin() ) { 125 if ( ! empty( $post->post_password ) ) { 126 127 /* translators: %s: Protected post title. */ 128 $prepend = __( 'Protected: %s' ); 129 130 /** 131 * Filters the text prepended to the post title for protected posts. 132 * 133 * The filter is only applied on the front end. 134 * 135 * @since 2.8.0 136 * 137 * @param string $prepend Text displayed before the post title. 138 * Default 'Protected: %s'. 139 * @param WP_Post $post Current post object. 140 */ 141 $protected_title_format = apply_filters( 'protected_title_format', $prepend, $post ); 142 143 $post_title = sprintf( $protected_title_format, $post_title ); 144 } elseif ( isset( $post->post_status ) && 'private' === $post->post_status ) { 145 146 /* translators: %s: Private post title. */ 147 $prepend = __( 'Private: %s' ); 148 149 /** 150 * Filters the text prepended to the post title of private posts. 151 * 152 * The filter is only applied on the front end. 153 * 154 * @since 2.8.0 155 * 156 * @param string $prepend Text displayed before the post title. 157 * Default 'Private: %s'. 158 * @param WP_Post $post Current post object. 159 */ 160 $private_title_format = apply_filters( 'private_title_format', $prepend, $post ); 161 162 $post_title = sprintf( $private_title_format, $post_title ); 163 } 164 } 165 166 /** 167 * Filters the post title. 168 * 169 * @since 0.71 170 * 171 * @param string $post_title The post title. 172 * @param int $post_id The post ID. 173 */ 174 return apply_filters( 'the_title', $post_title, $post_id ); 175 } 176 177 /** 178 * Displays the Post Global Unique Identifier (guid). 179 * 180 * The guid will appear to be a link, but should not be used as a link to the 181 * post. The reason you should not use it as a link, is because of moving the 182 * blog across domains. 183 * 184 * URL is escaped to make it XML-safe. 185 * 186 * @since 1.5.0 187 * 188 * @param int|WP_Post $post Optional. Post ID or post object. Default is global $post. 189 */ 190 function the_guid( $post = 0 ) { 191 $post = get_post( $post ); 192 193 $post_guid = isset( $post->guid ) ? get_the_guid( $post ) : ''; 194 $post_id = isset( $post->ID ) ? $post->ID : 0; 195 196 /** 197 * Filters the escaped Global Unique Identifier (guid) of the post. 198 * 199 * @since 4.2.0 200 * 201 * @see get_the_guid() 202 * 203 * @param string $post_guid Escaped Global Unique Identifier (guid) of the post. 204 * @param int $post_id The post ID. 205 */ 206 echo apply_filters( 'the_guid', $post_guid, $post_id ); 207 } 208 209 /** 210 * Retrieves the Post Global Unique Identifier (guid). 211 * 212 * The guid will appear to be a link, but should not be used as an link to the 213 * post. The reason you should not use it as a link, is because of moving the 214 * blog across domains. 215 * 216 * @since 1.5.0 217 * 218 * @param int|WP_Post $post Optional. Post ID or post object. Default is global $post. 219 * @return string 220 */ 221 function get_the_guid( $post = 0 ) { 222 $post = get_post( $post ); 223 224 $post_guid = isset( $post->guid ) ? $post->guid : ''; 225 $post_id = isset( $post->ID ) ? $post->ID : 0; 226 227 /** 228 * Filters the Global Unique Identifier (guid) of the post. 229 * 230 * @since 1.5.0 231 * 232 * @param string $post_guid Global Unique Identifier (guid) of the post. 233 * @param int $post_id The post ID. 234 */ 235 return apply_filters( 'get_the_guid', $post_guid, $post_id ); 236 } 237 238 /** 239 * Displays the post content. 240 * 241 * @since 0.71 242 * 243 * @param string $more_link_text Optional. Content for when there is more text. 244 * @param bool $strip_teaser Optional. Strip teaser content before the more text. Default false. 245 */ 246 function the_content( $more_link_text = null, $strip_teaser = false ) { 247 $content = get_the_content( $more_link_text, $strip_teaser ); 248 249 /** 250 * Filters the post content. 251 * 252 * @since 0.71 253 * 254 * @param string $content Content of the current post. 255 */ 256 $content = apply_filters( 'the_content', $content ); 257 $content = str_replace( ']]>', ']]>', $content ); 258 echo $content; 259 } 260 261 /** 262 * Retrieves the post content. 263 * 264 * @since 0.71 265 * @since 5.2.0 Added the `$post` parameter. 266 * 267 * @global int $page Page number of a single post/page. 268 * @global int $more Boolean indicator for whether single post/page is being viewed. 269 * @global bool $preview Whether post/page is in preview mode. 270 * @global array $pages Array of all pages in post/page. Each array element contains 271 * part of the content separated by the `<!--nextpage-->` tag. 272 * @global int $multipage Boolean indicator for whether multiple pages are in play. 273 * 274 * @param string $more_link_text Optional. Content for when there is more text. 275 * @param bool $strip_teaser Optional. Strip teaser content before the more text. Default false. 276 * @param WP_Post|object|int $post Optional. WP_Post instance or Post ID/object. Default null. 277 * @return string 278 */ 279 function get_the_content( $more_link_text = null, $strip_teaser = false, $post = null ) { 280 global $page, $more, $preview, $pages, $multipage; 281 282 $_post = get_post( $post ); 283 284 if ( ! ( $_post instanceof WP_Post ) ) { 285 return ''; 286 } 287 288 /* 289 * Use the globals if the $post parameter was not specified, 290 * but only after they have been set up in setup_postdata(). 291 */ 292 if ( null === $post && did_action( 'the_post' ) ) { 293 $elements = compact( 'page', 'more', 'preview', 'pages', 'multipage' ); 294 } else { 295 $elements = generate_postdata( $_post ); 296 } 297 298 if ( null === $more_link_text ) { 299 $more_link_text = sprintf( 300 '<span aria-label="%1$s">%2$s</span>', 301 sprintf( 302 /* translators: %s: Post title. */ 303 __( 'Continue reading %s' ), 304 the_title_attribute( 305 array( 306 'echo' => false, 307 'post' => $_post, 308 ) 309 ) 310 ), 311 __( '(more…)' ) 312 ); 313 } 314 315 $output = ''; 316 $has_teaser = false; 317 318 // If post password required and it doesn't match the cookie. 319 if ( post_password_required( $_post ) ) { 320 return get_the_password_form( $_post ); 321 } 322 323 // If the requested page doesn't exist. 324 if ( $elements['page'] > count( $elements['pages'] ) ) { 325 // Give them the highest numbered page that DOES exist. 326 $elements['page'] = count( $elements['pages'] ); 327 } 328 329 $page_no = $elements['page']; 330 $content = $elements['pages'][ $page_no - 1 ]; 331 if ( preg_match( '/<!--more(.*?)?-->/', $content, $matches ) ) { 332 if ( has_block( 'more', $content ) ) { 333 // Remove the core/more block delimiters. They will be left over after $content is split up. 334 $content = preg_replace( '/<!-- \/?wp:more(.*?) -->/', '', $content ); 335 } 336 337 $content = explode( $matches[0], $content, 2 ); 338 339 if ( ! empty( $matches[1] ) && ! empty( $more_link_text ) ) { 340 $more_link_text = strip_tags( wp_kses_no_null( trim( $matches[1] ) ) ); 341 } 342 343 $has_teaser = true; 344 } else { 345 $content = array( $content ); 346 } 347 348 if ( str_contains( $_post->post_content, '<!--noteaser-->' ) && ( ! $elements['multipage'] || 1 == $elements['page'] ) ) { 349 $strip_teaser = true; 350 } 351 352 $teaser = $content[0]; 353 354 if ( $elements['more'] && $strip_teaser && $has_teaser ) { 355 $teaser = ''; 356 } 357 358 $output .= $teaser; 359 360 if ( count( $content ) > 1 ) { 361 if ( $elements['more'] ) { 362 $output .= '<span id="more-' . $_post->ID . '"></span>' . $content[1]; 363 } else { 364 if ( ! empty( $more_link_text ) ) { 365 366 /** 367 * Filters the Read More link text. 368 * 369 * @since 2.8.0 370 * 371 * @param string $more_link_element Read More link element. 372 * @param string $more_link_text Read More text. 373 */ 374 $output .= apply_filters( 'the_content_more_link', ' <a href="' . get_permalink( $_post ) . "#more-{$_post->ID}\" class=\"more-link\">$more_link_text</a>", $more_link_text ); 375 } 376 $output = force_balance_tags( $output ); 377 } 378 } 379 380 return $output; 381 } 382 383 /** 384 * Displays the post excerpt. 385 * 386 * @since 0.71 387 */ 388 function the_excerpt() { 389 390 /** 391 * Filters the displayed post excerpt. 392 * 393 * @since 0.71 394 * 395 * @see get_the_excerpt() 396 * 397 * @param string $post_excerpt The post excerpt. 398 */ 399 echo apply_filters( 'the_excerpt', get_the_excerpt() ); 400 } 401 402 /** 403 * Retrieves the post excerpt. 404 * 405 * @since 0.71 406 * @since 4.5.0 Introduced the `$post` parameter. 407 * 408 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post. 409 * @return string Post excerpt. 410 */ 411 function get_the_excerpt( $post = null ) { 412 if ( is_bool( $post ) ) { 413 _deprecated_argument( __FUNCTION__, '2.3.0' ); 414 } 415 416 $post = get_post( $post ); 417 if ( empty( $post ) ) { 418 return ''; 419 } 420 421 if ( post_password_required( $post ) ) { 422 return __( 'There is no excerpt because this is a protected post.' ); 423 } 424 425 /** 426 * Filters the retrieved post excerpt. 427 * 428 * @since 1.2.0 429 * @since 4.5.0 Introduced the `$post` parameter. 430 * 431 * @param string $post_excerpt The post excerpt. 432 * @param WP_Post $post Post object. 433 */ 434 return apply_filters( 'get_the_excerpt', $post->post_excerpt, $post ); 435 } 436 437 /** 438 * Determines whether the post has a custom excerpt. 439 * 440 * For more information on this and similar theme functions, check out 441 * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ 442 * Conditional Tags} article in the Theme Developer Handbook. 443 * 444 * @since 2.3.0 445 * 446 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post. 447 * @return bool True if the post has a custom excerpt, false otherwise. 448 */ 449 function has_excerpt( $post = 0 ) { 450 $post = get_post( $post ); 451 return ( ! empty( $post->post_excerpt ) ); 452 } 453 454 /** 455 * Displays the classes for the post container element. 456 * 457 * @since 2.7.0 458 * 459 * @param string|string[] $css_class Optional. One or more classes to add to the class list. 460 * Default empty. 461 * @param int|WP_Post $post Optional. Post ID or post object. Defaults to the global `$post`. 462 */ 463 function post_class( $css_class = '', $post = null ) { 464 // Separates classes with a single space, collates classes for post DIV. 465 echo 'class="' . esc_attr( implode( ' ', get_post_class( $css_class, $post ) ) ) . '"'; 466 } 467 468 /** 469 * Retrieves an array of the class names for the post container element. 470 * 471 * The class names are many: 472 * 473 * - If the post has a post thumbnail, `has-post-thumbnail` is added as a class. 474 * - If the post is sticky, then the `sticky` class name is added. 475 * - The class `hentry` is always added to each post. 476 * - For each taxonomy that the post belongs to, a class will be added of the format 477 * `{$taxonomy}-{$slug}`, e.g. `category-foo` or `my_custom_taxonomy-bar`. 478 * The `post_tag` taxonomy is a special case; the class has the `tag-` prefix 479 * instead of `post_tag-`. 480 * 481 * All class names are passed through the filter, {@see 'post_class'}, followed by 482 * `$css_class` parameter value, with the post ID as the last parameter. 483 * 484 * @since 2.7.0 485 * @since 4.2.0 Custom taxonomy class names were added. 486 * 487 * @param string|string[] $css_class Optional. Space-separated string or array of class names 488 * to add to the class list. Default empty. 489 * @param int|WP_Post $post Optional. Post ID or post object. 490 * @return string[] Array of class names. 491 */ 492 function get_post_class( $css_class = '', $post = null ) { 493 $post = get_post( $post ); 494 495 $classes = array(); 496 497 if ( $css_class ) { 498 if ( ! is_array( $css_class ) ) { 499 $css_class = preg_split( '#\s+#', $css_class ); 500 } 501 $classes = array_map( 'esc_attr', $css_class ); 502 } else { 503 // Ensure that we always coerce class to being an array. 504 $css_class = array(); 505 } 506 507 if ( ! $post ) { 508 return $classes; 509 } 510 511 $classes[] = 'post-' . $post->ID; 512 if ( ! is_admin() ) { 513 $classes[] = $post->post_type; 514 } 515 $classes[] = 'type-' . $post->post_type; 516 $classes[] = 'status-' . $post->post_status; 517 518 // Post Format. 519 if ( post_type_supports( $post->post_type, 'post-formats' ) ) { 520 $post_format = get_post_format( $post->ID ); 521 522 if ( $post_format && ! is_wp_error( $post_format ) ) { 523 $classes[] = 'format-' . sanitize_html_class( $post_format ); 524 } else { 525 $classes[] = 'format-standard'; 526 } 527 } 528 529 $post_password_required = post_password_required( $post->ID ); 530 531 // Post requires password. 532 if ( $post_password_required ) { 533 $classes[] = 'post-password-required'; 534 } elseif ( ! empty( $post->post_password ) ) { 535 $classes[] = 'post-password-protected'; 536 } 537 538 // Post thumbnails. 539 if ( current_theme_supports( 'post-thumbnails' ) && has_post_thumbnail( $post->ID ) && ! is_attachment( $post ) && ! $post_password_required ) { 540 $classes[] = 'has-post-thumbnail'; 541 } 542 543 // Sticky for Sticky Posts. 544 if ( is_sticky( $post->ID ) ) { 545 if ( is_home() && ! is_paged() ) { 546 $classes[] = 'sticky'; 547 } elseif ( is_admin() ) { 548 $classes[] = 'status-sticky'; 549 } 550 } 551 552 // hentry for hAtom compliance. 553 $classes[] = 'hentry'; 554 555 // All public taxonomies. 556 $taxonomies = get_taxonomies( array( 'public' => true ) ); 557 558 /** 559 * Filters the taxonomies to generate classes for each individual term. 560 * 561 * Default is all public taxonomies registered to the post type. 562 * 563 * @since 6.1.0 564 * 565 * @param string[] $taxonomies List of all taxonomy names to generate classes for. 566 * @param int $post_id The post ID. 567 * @param string[] $classes An array of post class names. 568 * @param string[] $css_class An array of additional class names added to the post. 569 */ 570 $taxonomies = apply_filters( 'post_class_taxonomies', $taxonomies, $post->ID, $classes, $css_class ); 571 572 foreach ( (array) $taxonomies as $taxonomy ) { 573 if ( is_object_in_taxonomy( $post->post_type, $taxonomy ) ) { 574 foreach ( (array) get_the_terms( $post->ID, $taxonomy ) as $term ) { 575 if ( empty( $term->slug ) ) { 576 continue; 577 } 578 579 $term_class = sanitize_html_class( $term->slug, $term->term_id ); 580 if ( is_numeric( $term_class ) || ! trim( $term_class, '-' ) ) { 581 $term_class = $term->term_id; 582 } 583 584 // 'post_tag' uses the 'tag' prefix for backward compatibility. 585 if ( 'post_tag' === $taxonomy ) { 586 $classes[] = 'tag-' . $term_class; 587 } else { 588 $classes[] = sanitize_html_class( $taxonomy . '-' . $term_class, $taxonomy . '-' . $term->term_id ); 589 } 590 } 591 } 592 } 593 594 $classes = array_map( 'esc_attr', $classes ); 595 596 /** 597 * Filters the list of CSS class names for the current post. 598 * 599 * @since 2.7.0 600 * 601 * @param string[] $classes An array of post class names. 602 * @param string[] $css_class An array of additional class names added to the post. 603 * @param int $post_id The post ID. 604 */ 605 $classes = apply_filters( 'post_class', $classes, $css_class, $post->ID ); 606 607 return array_unique( $classes ); 608 } 609 610 /** 611 * Displays the class names for the body element. 612 * 613 * @since 2.8.0 614 * 615 * @param string|string[] $css_class Optional. Space-separated string or array of class names 616 * to add to the class list. Default empty. 617 */ 618 function body_class( $css_class = '' ) { 619 // Separates class names with a single space, collates class names for body element. 620 echo 'class="' . esc_attr( implode( ' ', get_body_class( $css_class ) ) ) . '"'; 621 } 622 623 /** 624 * Retrieves an array of the class names for the body element. 625 * 626 * @since 2.8.0 627 * 628 * @global WP_Query $wp_query WordPress Query object. 629 * 630 * @param string|string[] $css_class Optional. Space-separated string or array of class names 631 * to add to the class list. Default empty. 632 * @return string[] Array of class names. 633 */ 634 function get_body_class( $css_class = '' ) { 635 global $wp_query; 636 637 $classes = array(); 638 639 if ( is_rtl() ) { 640 $classes[] = 'rtl'; 641 } 642 643 if ( is_front_page() ) { 644 $classes[] = 'home'; 645 } 646 if ( is_home() ) { 647 $classes[] = 'blog'; 648 } 649 if ( is_privacy_policy() ) { 650 $classes[] = 'privacy-policy'; 651 } 652 if ( is_archive() ) { 653 $classes[] = 'archive'; 654 } 655 if ( is_date() ) { 656 $classes[] = 'date'; 657 } 658 if ( is_search() ) { 659 $classes[] = 'search'; 660 $classes[] = $wp_query->posts ? 'search-results' : 'search-no-results'; 661 } 662 if ( is_paged() ) { 663 $classes[] = 'paged'; 664 } 665 if ( is_attachment() ) { 666 $classes[] = 'attachment'; 667 } 668 if ( is_404() ) { 669 $classes[] = 'error404'; 670 } 671 672 if ( is_singular() ) { 673 $post = $wp_query->get_queried_object(); 674 $post_id = $post->ID; 675 $post_type = $post->post_type; 676 677 if ( is_page_template() ) { 678 $classes[] = "{$post_type}-template"; 679 680 $template_slug = get_page_template_slug( $post_id ); 681 $template_parts = explode( '/', $template_slug ); 682 683 foreach ( $template_parts as $part ) { 684 $classes[] = "{$post_type}-template-" . sanitize_html_class( str_replace( array( '.', '/' ), '-', basename( $part, '.php' ) ) ); 685 } 686 $classes[] = "{$post_type}-template-" . sanitize_html_class( str_replace( '.', '-', $template_slug ) ); 687 } else { 688 $classes[] = "{$post_type}-template-default"; 689 } 690 691 if ( is_single() ) { 692 $classes[] = 'single'; 693 if ( isset( $post->post_type ) ) { 694 $classes[] = 'single-' . sanitize_html_class( $post->post_type, $post_id ); 695 $classes[] = 'postid-' . $post_id; 696 697 // Post Format. 698 if ( post_type_supports( $post->post_type, 'post-formats' ) ) { 699 $post_format = get_post_format( $post->ID ); 700 701 if ( $post_format && ! is_wp_error( $post_format ) ) { 702 $classes[] = 'single-format-' . sanitize_html_class( $post_format ); 703 } else { 704 $classes[] = 'single-format-standard'; 705 } 706 } 707 } 708 } 709 710 if ( is_attachment() ) { 711 $mime_type = get_post_mime_type( $post_id ); 712 $mime_prefix = array( 'application/', 'image/', 'text/', 'audio/', 'video/', 'music/' ); 713 $classes[] = 'attachmentid-' . $post_id; 714 $classes[] = 'attachment-' . str_replace( $mime_prefix, '', $mime_type ); 715 } elseif ( is_page() ) { 716 $classes[] = 'page'; 717 $classes[] = 'page-id-' . $post_id; 718 719 if ( get_pages( 720 array( 721 'parent' => $post_id, 722 'number' => 1, 723 ) 724 ) ) { 725 $classes[] = 'page-parent'; 726 } 727 728 if ( $post->post_parent ) { 729 $classes[] = 'page-child'; 730 $classes[] = 'parent-pageid-' . $post->post_parent; 731 } 732 } 733 } elseif ( is_archive() ) { 734 if ( is_post_type_archive() ) { 735 $classes[] = 'post-type-archive'; 736 $post_type = get_query_var( 'post_type' ); 737 if ( is_array( $post_type ) ) { 738 $post_type = reset( $post_type ); 739 } 740 $classes[] = 'post-type-archive-' . sanitize_html_class( $post_type ); 741 } elseif ( is_author() ) { 742 $author = $wp_query->get_queried_object(); 743 $classes[] = 'author'; 744 if ( isset( $author->user_nicename ) ) { 745 $classes[] = 'author-' . sanitize_html_class( $author->user_nicename, $author->ID ); 746 $classes[] = 'author-' . $author->ID; 747 } 748 } elseif ( is_category() ) { 749 $cat = $wp_query->get_queried_object(); 750 $classes[] = 'category'; 751 if ( isset( $cat->term_id ) ) { 752 $cat_class = sanitize_html_class( $cat->slug, $cat->term_id ); 753 if ( is_numeric( $cat_class ) || ! trim( $cat_class, '-' ) ) { 754 $cat_class = $cat->term_id; 755 } 756 757 $classes[] = 'category-' . $cat_class; 758 $classes[] = 'category-' . $cat->term_id; 759 } 760 } elseif ( is_tag() ) { 761 $tag = $wp_query->get_queried_object(); 762 $classes[] = 'tag'; 763 if ( isset( $tag->term_id ) ) { 764 $tag_class = sanitize_html_class( $tag->slug, $tag->term_id ); 765 if ( is_numeric( $tag_class ) || ! trim( $tag_class, '-' ) ) { 766 $tag_class = $tag->term_id; 767 } 768 769 $classes[] = 'tag-' . $tag_class; 770 $classes[] = 'tag-' . $tag->term_id; 771 } 772 } elseif ( is_tax() ) { 773 $term = $wp_query->get_queried_object(); 774 if ( isset( $term->term_id ) ) { 775 $term_class = sanitize_html_class( $term->slug, $term->term_id ); 776 if ( is_numeric( $term_class ) || ! trim( $term_class, '-' ) ) { 777 $term_class = $term->term_id; 778 } 779 780 $classes[] = 'tax-' . sanitize_html_class( $term->taxonomy ); 781 $classes[] = 'term-' . $term_class; 782 $classes[] = 'term-' . $term->term_id; 783 } 784 } 785 } 786 787 if ( is_user_logged_in() ) { 788 $classes[] = 'logged-in'; 789 } 790 791 if ( is_admin_bar_showing() ) { 792 $classes[] = 'admin-bar'; 793 $classes[] = 'no-customize-support'; 794 } 795 796 if ( current_theme_supports( 'custom-background' ) 797 && ( get_background_color() !== get_theme_support( 'custom-background', 'default-color' ) || get_background_image() ) ) { 798 $classes[] = 'custom-background'; 799 } 800 801 if ( has_custom_logo() ) { 802 $classes[] = 'wp-custom-logo'; 803 } 804 805 if ( current_theme_supports( 'responsive-embeds' ) ) { 806 $classes[] = 'wp-embed-responsive'; 807 } 808 809 $page = $wp_query->get( 'page' ); 810 811 if ( ! $page || $page < 2 ) { 812 $page = $wp_query->get( 'paged' ); 813 } 814 815 if ( $page && $page > 1 && ! is_404() ) { 816 $classes[] = 'paged-' . $page; 817 818 if ( is_single() ) { 819 $classes[] = 'single-paged-' . $page; 820 } elseif ( is_page() ) { 821 $classes[] = 'page-paged-' . $page; 822 } elseif ( is_category() ) { 823 $classes[] = 'category-paged-' . $page; 824 } elseif ( is_tag() ) { 825 $classes[] = 'tag-paged-' . $page; 826 } elseif ( is_date() ) { 827 $classes[] = 'date-paged-' . $page; 828 } elseif ( is_author() ) { 829 $classes[] = 'author-paged-' . $page; 830 } elseif ( is_search() ) { 831 $classes[] = 'search-paged-' . $page; 832 } elseif ( is_post_type_archive() ) { 833 $classes[] = 'post-type-paged-' . $page; 834 } 835 } 836 837 if ( ! empty( $css_class ) ) { 838 if ( ! is_array( $css_class ) ) { 839 $css_class = preg_split( '#\s+#', $css_class ); 840 } 841 $classes = array_merge( $classes, $css_class ); 842 } else { 843 // Ensure that we always coerce class to being an array. 844 $css_class = array(); 845 } 846 847 $classes = array_map( 'esc_attr', $classes ); 848 849 /** 850 * Filters the list of CSS body class names for the current post or page. 851 * 852 * @since 2.8.0 853 * 854 * @param string[] $classes An array of body class names. 855 * @param string[] $css_class An array of additional class names added to the body. 856 */ 857 $classes = apply_filters( 'body_class', $classes, $css_class ); 858 859 return array_unique( $classes ); 860 } 861 862 /** 863 * Determines whether the post requires password and whether a correct password has been provided. 864 * 865 * @since 2.7.0 866 * 867 * @param int|WP_Post|null $post An optional post. Global $post used if not provided. 868 * @return bool false if a password is not required or the correct password cookie is present, true otherwise. 869 */ 870 function post_password_required( $post = null ) { 871 $post = get_post( $post ); 872 873 if ( empty( $post->post_password ) ) { 874 /** This filter is documented in wp-includes/post-template.php */ 875 return apply_filters( 'post_password_required', false, $post ); 876 } 877 878 if ( ! isset( $_COOKIE[ 'wp-postpass_' . COOKIEHASH ] ) ) { 879 /** This filter is documented in wp-includes/post-template.php */ 880 return apply_filters( 'post_password_required', true, $post ); 881 } 882 883 require_once ABSPATH . WPINC . '/class-phpass.php'; 884 $hasher = new PasswordHash( 8, true ); 885 886 $hash = wp_unslash( $_COOKIE[ 'wp-postpass_' . COOKIEHASH ] ); 887 if ( ! str_starts_with( $hash, '$P$B' ) ) { 888 $required = true; 889 } else { 890 $required = ! $hasher->CheckPassword( $post->post_password, $hash ); 891 } 892 893 /** 894 * Filters whether a post requires the user to supply a password. 895 * 896 * @since 4.7.0 897 * 898 * @param bool $required Whether the user needs to supply a password. True if password has not been 899 * provided or is incorrect, false if password has been supplied or is not required. 900 * @param WP_Post $post Post object. 901 */ 902 return apply_filters( 'post_password_required', $required, $post ); 903 } 904 905 // 906 // Page Template Functions for usage in Themes. 907 // 908 909 /** 910 * The formatted output of a list of pages. 911 * 912 * Displays page links for paginated posts (i.e. including the `<!--nextpage-->` 913 * Quicktag one or more times). This tag must be within The Loop. 914 * 915 * @since 1.2.0 916 * @since 5.1.0 Added the `aria_current` argument. 917 * 918 * @global int $page 919 * @global int $numpages 920 * @global int $multipage 921 * @global int $more 922 * 923 * @param string|array $args { 924 * Optional. Array or string of default arguments. 925 * 926 * @type string $before HTML or text to prepend to each link. Default is `<p> Pages:`. 927 * @type string $after HTML or text to append to each link. Default is `</p>`. 928 * @type string $link_before HTML or text to prepend to each link, inside the `<a>` tag. 929 * Also prepended to the current item, which is not linked. Default empty. 930 * @type string $link_after HTML or text to append to each Pages link inside the `<a>` tag. 931 * Also appended to the current item, which is not linked. Default empty. 932 * @type string $aria_current The value for the aria-current attribute. Possible values are 'page', 933 * 'step', 'location', 'date', 'time', 'true', 'false'. Default is 'page'. 934 * @type string $next_or_number Indicates whether page numbers should be used. Valid values are number 935 * and next. Default is 'number'. 936 * @type string $separator Text between pagination links. Default is ' '. 937 * @type string $nextpagelink Link text for the next page link, if available. Default is 'Next Page'. 938 * @type string $previouspagelink Link text for the previous page link, if available. Default is 'Previous Page'. 939 * @type string $pagelink Format string for page numbers. The % in the parameter string will be 940 * replaced with the page number, so 'Page %' generates "Page 1", "Page 2", etc. 941 * Defaults to '%', just the page number. 942 * @type int|bool $echo Whether to echo or not. Accepts 1|true or 0|false. Default 1|true. 943 * } 944 * @return string Formatted output in HTML. 945 */ 946 function wp_link_pages( $args = '' ) { 947 global $page, $numpages, $multipage, $more; 948 949 $defaults = array( 950 'before' => '<p class="post-nav-links">' . __( 'Pages:' ), 951 'after' => '</p>', 952 'link_before' => '', 953 'link_after' => '', 954 'aria_current' => 'page', 955 'next_or_number' => 'number', 956 'separator' => ' ', 957 'nextpagelink' => __( 'Next page' ), 958 'previouspagelink' => __( 'Previous page' ), 959 'pagelink' => '%', 960 'echo' => 1, 961 ); 962 963 $parsed_args = wp_parse_args( $args, $defaults ); 964 965 /** 966 * Filters the arguments used in retrieving page links for paginated posts. 967 * 968 * @since 3.0.0 969 * 970 * @param array $parsed_args An array of page link arguments. See wp_link_pages() 971 * for information on accepted arguments. 972 */ 973 $parsed_args = apply_filters( 'wp_link_pages_args', $parsed_args ); 974 975 $output = ''; 976 if ( $multipage ) { 977 if ( 'number' === $parsed_args['next_or_number'] ) { 978 $output .= $parsed_args['before']; 979 for ( $i = 1; $i <= $numpages; $i++ ) { 980 $link = $parsed_args['link_before'] . str_replace( '%', $i, $parsed_args['pagelink'] ) . $parsed_args['link_after']; 981 if ( $i != $page || ! $more && 1 == $page ) { 982 $link = _wp_link_page( $i ) . $link . '</a>'; 983 } elseif ( $i === $page ) { 984 $link = '<span class="post-page-numbers current" aria-current="' . esc_attr( $parsed_args['aria_current'] ) . '">' . $link . '</span>'; 985 } 986 /** 987 * Filters the HTML output of individual page number links. 988 * 989 * @since 3.6.0 990 * 991 * @param string $link The page number HTML output. 992 * @param int $i Page number for paginated posts' page links. 993 */ 994 $link = apply_filters( 'wp_link_pages_link', $link, $i ); 995 996 // Use the custom links separator beginning with the second link. 997 $output .= ( 1 === $i ) ? ' ' : $parsed_args['separator']; 998 $output .= $link; 999 } 1000 $output .= $parsed_args['after']; 1001 } elseif ( $more ) { 1002 $output .= $parsed_args['before']; 1003 $prev = $page - 1; 1004 if ( $prev > 0 ) { 1005 $link = _wp_link_page( $prev ) . $parsed_args['link_before'] . $parsed_args['previouspagelink'] . $parsed_args['link_after'] . '</a>'; 1006 1007 /** This filter is documented in wp-includes/post-template.php */ 1008 $output .= apply_filters( 'wp_link_pages_link', $link, $prev ); 1009 } 1010 $next = $page + 1; 1011 if ( $next <= $numpages ) { 1012 if ( $prev ) { 1013 $output .= $parsed_args['separator']; 1014 } 1015 $link = _wp_link_page( $next ) . $parsed_args['link_before'] . $parsed_args['nextpagelink'] . $parsed_args['link_after'] . '</a>'; 1016 1017 /** This filter is documented in wp-includes/post-template.php */ 1018 $output .= apply_filters( 'wp_link_pages_link', $link, $next ); 1019 } 1020 $output .= $parsed_args['after']; 1021 } 1022 } 1023 1024 /** 1025 * Filters the HTML output of page links for paginated posts. 1026 * 1027 * @since 3.6.0 1028 * 1029 * @param string $output HTML output of paginated posts' page links. 1030 * @param array|string $args An array or query string of arguments. See wp_link_pages() 1031 * for information on accepted arguments. 1032 */ 1033 $html = apply_filters( 'wp_link_pages', $output, $args ); 1034 1035 if ( $parsed_args['echo'] ) { 1036 echo $html; 1037 } 1038 return $html; 1039 } 1040 1041 /** 1042 * Helper function for wp_link_pages(). 1043 * 1044 * @since 3.1.0 1045 * @access private 1046 * 1047 * @global WP_Rewrite $wp_rewrite WordPress rewrite component. 1048 * 1049 * @param int $i Page number. 1050 * @return string Link. 1051 */ 1052 function _wp_link_page( $i ) { 1053 global $wp_rewrite; 1054 $post = get_post(); 1055 $query_args = array(); 1056 1057 if ( 1 == $i ) { 1058 $url = get_permalink(); 1059 } else { 1060 if ( ! get_option( 'permalink_structure' ) || in_array( $post->post_status, array( 'draft', 'pending' ), true ) ) { 1061 $url = add_query_arg( 'page', $i, get_permalink() ); 1062 } elseif ( 'page' === get_option( 'show_on_front' ) && get_option( 'page_on_front' ) == $post->ID ) { 1063 $url = trailingslashit( get_permalink() ) . user_trailingslashit( "$wp_rewrite->pagination_base/" . $i, 'single_paged' ); 1064 } else { 1065 $url = trailingslashit( get_permalink() ) . user_trailingslashit( $i, 'single_paged' ); 1066 } 1067 } 1068 1069 if ( is_preview() ) { 1070 1071 if ( ( 'draft' !== $post->post_status ) && isset( $_GET['preview_id'], $_GET['preview_nonce'] ) ) { 1072 $query_args['preview_id'] = wp_unslash( $_GET['preview_id'] ); 1073 $query_args['preview_nonce'] = wp_unslash( $_GET['preview_nonce'] ); 1074 } 1075 1076 $url = get_preview_post_link( $post, $query_args, $url ); 1077 } 1078 1079 return '<a href="' . esc_url( $url ) . '" class="post-page-numbers">'; 1080 } 1081 1082 // 1083 // Post-meta: Custom per-post fields. 1084 // 1085 1086 /** 1087 * Retrieves post custom meta data field. 1088 * 1089 * @since 1.5.0 1090 * 1091 * @param string $key Meta data key name. 1092 * @return array|string|false Array of values, or single value if only one element exists. 1093 * False if the key does not exist. 1094 */ 1095 function post_custom( $key = '' ) { 1096 $custom = get_post_custom(); 1097 1098 if ( ! isset( $custom[ $key ] ) ) { 1099 return false; 1100 } elseif ( 1 === count( $custom[ $key ] ) ) { 1101 return $custom[ $key ][0]; 1102 } else { 1103 return $custom[ $key ]; 1104 } 1105 } 1106 1107 /** 1108 * Displays a list of post custom fields. 1109 * 1110 * @since 1.2.0 1111 * 1112 * @deprecated 6.0.2 Use get_post_meta() to retrieve post meta and render manually. 1113 */ 1114 function the_meta() { 1115 _deprecated_function( __FUNCTION__, '6.0.2', 'get_post_meta()' ); 1116 $keys = get_post_custom_keys(); 1117 if ( $keys ) { 1118 $li_html = ''; 1119 foreach ( (array) $keys as $key ) { 1120 $keyt = trim( $key ); 1121 if ( is_protected_meta( $keyt, 'post' ) ) { 1122 continue; 1123 } 1124 1125 $values = array_map( 'trim', get_post_custom_values( $key ) ); 1126 $value = implode( ', ', $values ); 1127 1128 $html = sprintf( 1129 "<li><span class='post-meta-key'>%s</span> %s</li>\n", 1130 /* translators: %s: Post custom field name. */ 1131 esc_html( sprintf( _x( '%s:', 'Post custom field name' ), $key ) ), 1132 esc_html( $value ) 1133 ); 1134 1135 /** 1136 * Filters the HTML output of the li element in the post custom fields list. 1137 * 1138 * @since 2.2.0 1139 * 1140 * @param string $html The HTML output for the li element. 1141 * @param string $key Meta key. 1142 * @param string $value Meta value. 1143 */ 1144 $li_html .= apply_filters( 'the_meta_key', $html, $key, $value ); 1145 } 1146 1147 if ( $li_html ) { 1148 echo "<ul class='post-meta'>\n{$li_html}</ul>\n"; 1149 } 1150 } 1151 } 1152 1153 // 1154 // Pages. 1155 // 1156 1157 /** 1158 * Retrieves or displays a list of pages as a dropdown (select list). 1159 * 1160 * @since 2.1.0 1161 * @since 4.2.0 The `$value_field` argument was added. 1162 * @since 4.3.0 The `$class` argument was added. 1163 * 1164 * @see get_pages() 1165 * 1166 * @param array|string $args { 1167 * Optional. Array or string of arguments to generate a page dropdown. See get_pages() for additional arguments. 1168 * 1169 * @type int $depth Maximum depth. Default 0. 1170 * @type int $child_of Page ID to retrieve child pages of. Default 0. 1171 * @type int|string $selected Value of the option that should be selected. Default 0. 1172 * @type bool|int $echo Whether to echo or return the generated markup. Accepts 0, 1, 1173 * or their bool equivalents. Default 1. 1174 * @type string $name Value for the 'name' attribute of the select element. 1175 * Default 'page_id'. 1176 * @type string $id Value for the 'id' attribute of the select element. 1177 * @type string $class Value for the 'class' attribute of the select element. Default: none. 1178 * Defaults to the value of `$name`. 1179 * @type string $show_option_none Text to display for showing no pages. Default empty (does not display). 1180 * @type string $show_option_no_change Text to display for "no change" option. Default empty (does not display). 1181 * @type string $option_none_value Value to use when no page is selected. Default empty. 1182 * @type string $value_field Post field used to populate the 'value' attribute of the option 1183 * elements. Accepts any valid post field. Default 'ID'. 1184 * } 1185 * @return string HTML dropdown list of pages. 1186 */ 1187 function wp_dropdown_pages( $args = '' ) { 1188 $defaults = array( 1189 'depth' => 0, 1190 'child_of' => 0, 1191 'selected' => 0, 1192 'echo' => 1, 1193 'name' => 'page_id', 1194 'id' => '', 1195 'class' => '', 1196 'show_option_none' => '', 1197 'show_option_no_change' => '', 1198 'option_none_value' => '', 1199 'value_field' => 'ID', 1200 ); 1201 1202 $parsed_args = wp_parse_args( $args, $defaults ); 1203 1204 $pages = get_pages( $parsed_args ); 1205 $output = ''; 1206 // Back-compat with old system where both id and name were based on $name argument. 1207 if ( empty( $parsed_args['id'] ) ) { 1208 $parsed_args['id'] = $parsed_args['name']; 1209 } 1210 1211 if ( ! empty( $pages ) ) { 1212 $class = ''; 1213 if ( ! empty( $parsed_args['class'] ) ) { 1214 $class = " class='" . esc_attr( $parsed_args['class'] ) . "'"; 1215 } 1216 1217 $output = "<select name='" . esc_attr( $parsed_args['name'] ) . "'" . $class . " id='" . esc_attr( $parsed_args['id'] ) . "'>\n"; 1218 if ( $parsed_args['show_option_no_change'] ) { 1219 $output .= "\t<option value=\"-1\">" . $parsed_args['show_option_no_change'] . "</option>\n"; 1220 } 1221 if ( $parsed_args['show_option_none'] ) { 1222 $output .= "\t<option value=\"" . esc_attr( $parsed_args['option_none_value'] ) . '">' . $parsed_args['show_option_none'] . "</option>\n"; 1223 } 1224 $output .= walk_page_dropdown_tree( $pages, $parsed_args['depth'], $parsed_args ); 1225 $output .= "</select>\n"; 1226 } 1227 1228 /** 1229 * Filters the HTML output of a list of pages as a dropdown. 1230 * 1231 * @since 2.1.0 1232 * @since 4.4.0 `$parsed_args` and `$pages` added as arguments. 1233 * 1234 * @param string $output HTML output for dropdown list of pages. 1235 * @param array $parsed_args The parsed arguments array. See wp_dropdown_pages() 1236 * for information on accepted arguments. 1237 * @param WP_Post[] $pages Array of the page objects. 1238 */ 1239 $html = apply_filters( 'wp_dropdown_pages', $output, $parsed_args, $pages ); 1240 1241 if ( $parsed_args['echo'] ) { 1242 echo $html; 1243 } 1244 1245 return $html; 1246 } 1247 1248 /** 1249 * Retrieves or displays a list of pages (or hierarchical post type items) in list (li) format. 1250 * 1251 * @since 1.5.0 1252 * @since 4.7.0 Added the `item_spacing` argument. 1253 * 1254 * @see get_pages() 1255 * 1256 * @global WP_Query $wp_query WordPress Query object. 1257 * 1258 * @param array|string $args { 1259 * Optional. Array or string of arguments to generate a list of pages. See get_pages() for additional arguments. 1260 * 1261 * @type int $child_of Display only the sub-pages of a single page by ID. Default 0 (all pages). 1262 * @type string $authors Comma-separated list of author IDs. Default empty (all authors). 1263 * @type string $date_format PHP date format to use for the listed pages. Relies on the 'show_date' parameter. 1264 * Default is the value of 'date_format' option. 1265 * @type int $depth Number of levels in the hierarchy of pages to include in the generated list. 1266 * Accepts -1 (any depth), 0 (all pages), 1 (top-level pages only), and n (pages to 1267 * the given n depth). Default 0. 1268 * @type bool $echo Whether or not to echo the list of pages. Default true. 1269 * @type string $exclude Comma-separated list of page IDs to exclude. Default empty. 1270 * @type array $include Comma-separated list of page IDs to include. Default empty. 1271 * @type string $link_after Text or HTML to follow the page link label. Default null. 1272 * @type string $link_before Text or HTML to precede the page link label. Default null. 1273 * @type string $post_type Post type to query for. Default 'page'. 1274 * @type string|array $post_status Comma-separated list or array of post statuses to include. Default 'publish'. 1275 * @type string $show_date Whether to display the page publish or modified date for each page. Accepts 1276 * 'modified' or any other value. An empty value hides the date. Default empty. 1277 * @type string $sort_column Comma-separated list of column names to sort the pages by. Accepts 'post_author', 1278 * 'post_date', 'post_title', 'post_name', 'post_modified', 'post_modified_gmt', 1279 * 'menu_order', 'post_parent', 'ID', 'rand', or 'comment_count'. Default 'post_title'. 1280 * @type string $title_li List heading. Passing a null or empty value will result in no heading, and the list 1281 * will not be wrapped with unordered list `<ul>` tags. Default 'Pages'. 1282 * @type string $item_spacing Whether to preserve whitespace within the menu's HTML. Accepts 'preserve' or 'discard'. 1283 * Default 'preserve'. 1284 * @type Walker $walker Walker instance to use for listing pages. Default empty which results in a 1285 * Walker_Page instance being used. 1286 * } 1287 * @return void|string Void if 'echo' argument is true, HTML list of pages if 'echo' is false. 1288 */ 1289 function wp_list_pages( $args = '' ) { 1290 $defaults = array( 1291 'depth' => 0, 1292 'show_date' => '', 1293 'date_format' => get_option( 'date_format' ), 1294 'child_of' => 0, 1295 'exclude' => '', 1296 'title_li' => __( 'Pages' ), 1297 'echo' => 1, 1298 'authors' => '', 1299 'sort_column' => 'menu_order, post_title', 1300 'link_before' => '', 1301 'link_after' => '', 1302 'item_spacing' => 'preserve', 1303 'walker' => '', 1304 ); 1305 1306 $parsed_args = wp_parse_args( $args, $defaults ); 1307 1308 if ( ! in_array( $parsed_args['item_spacing'], array( 'preserve', 'discard' ), true ) ) { 1309 // Invalid value, fall back to default. 1310 $parsed_args['item_spacing'] = $defaults['item_spacing']; 1311 } 1312 1313 $output = ''; 1314 $current_page = 0; 1315 1316 // Sanitize, mostly to keep spaces out. 1317 $parsed_args['exclude'] = preg_replace( '/[^0-9,]/', '', $parsed_args['exclude'] ); 1318 1319 // Allow plugins to filter an array of excluded pages (but don't put a nullstring into the array). 1320 $exclude_array = ( $parsed_args['exclude'] ) ? explode( ',', $parsed_args['exclude'] ) : array(); 1321 1322 /** 1323 * Filters the array of pages to exclude from the pages list. 1324 * 1325 * @since 2.1.0 1326 * 1327 * @param string[] $exclude_array An array of page IDs to exclude. 1328 */ 1329 $parsed_args['exclude'] = implode( ',', apply_filters( 'wp_list_pages_excludes', $exclude_array ) ); 1330 1331 $parsed_args['hierarchical'] = 0; 1332 1333 // Query pages. 1334 $pages = get_pages( $parsed_args ); 1335 1336 if ( ! empty( $pages ) ) { 1337 if ( $parsed_args['title_li'] ) { 1338 $output .= '<li class="pagenav">' . $parsed_args['title_li'] . '<ul>'; 1339 } 1340 global $wp_query; 1341 if ( is_page() || is_attachment() || $wp_query->is_posts_page ) { 1342 $current_page = get_queried_object_id(); 1343 } elseif ( is_singular() ) { 1344 $queried_object = get_queried_object(); 1345 if ( is_post_type_hierarchical( $queried_object->post_type ) ) { 1346 $current_page = $queried_object->ID; 1347 } 1348 } 1349 1350 $output .= walk_page_tree( $pages, $parsed_args['depth'], $current_page, $parsed_args ); 1351 1352 if ( $parsed_args['title_li'] ) { 1353 $output .= '</ul></li>'; 1354 } 1355 } 1356 1357 /** 1358 * Filters the HTML output of the pages to list. 1359 * 1360 * @since 1.5.1 1361 * @since 4.4.0 `$pages` added as arguments. 1362 * 1363 * @see wp_list_pages() 1364 * 1365 * @param string $output HTML output of the pages list. 1366 * @param array $parsed_args An array of page-listing arguments. See wp_list_pages() 1367 * for information on accepted arguments. 1368 * @param WP_Post[] $pages Array of the page objects. 1369 */ 1370 $html = apply_filters( 'wp_list_pages', $output, $parsed_args, $pages ); 1371 1372 if ( $parsed_args['echo'] ) { 1373 echo $html; 1374 } else { 1375 return $html; 1376 } 1377 } 1378 1379 /** 1380 * Displays or retrieves a list of pages with an optional home link. 1381 * 1382 * The arguments are listed below and part of the arguments are for wp_list_pages() function. 1383 * Check that function for more info on those arguments. 1384 * 1385 * @since 2.7.0 1386 * @since 4.4.0 Added `menu_id`, `container`, `before`, `after`, and `walker` arguments. 1387 * @since 4.7.0 Added the `item_spacing` argument. 1388 * 1389 * @param array|string $args { 1390 * Optional. Array or string of arguments to generate a page menu. See wp_list_pages() for additional arguments. 1391 * 1392 * @type string $sort_column How to sort the list of pages. Accepts post column names. 1393 * Default 'menu_order, post_title'. 1394 * @type string $menu_id ID for the div containing the page list. Default is empty string. 1395 * @type string $menu_class Class to use for the element containing the page list. Default 'menu'. 1396 * @type string $container Element to use for the element containing the page list. Default 'div'. 1397 * @type bool $echo Whether to echo the list or return it. Accepts true (echo) or false (return). 1398 * Default true. 1399 * @type int|bool|string $show_home Whether to display the link to the home page. Can just enter the text 1400 * you'd like shown for the home link. 1|true defaults to 'Home'. 1401 * @type string $link_before The HTML or text to prepend to $show_home text. Default empty. 1402 * @type string $link_after The HTML or text to append to $show_home text. Default empty. 1403 * @type string $before The HTML or text to prepend to the menu. Default is '<ul>'. 1404 * @type string $after The HTML or text to append to the menu. Default is '</ul>'. 1405 * @type string $item_spacing Whether to preserve whitespace within the menu's HTML. Accepts 'preserve' 1406 * or 'discard'. Default 'discard'. 1407 * @type Walker $walker Walker instance to use for listing pages. Default empty which results in a 1408 * Walker_Page instance being used. 1409 * } 1410 * @return void|string Void if 'echo' argument is true, HTML menu if 'echo' is false. 1411 */ 1412 function wp_page_menu( $args = array() ) { 1413 $defaults = array( 1414 'sort_column' => 'menu_order, post_title', 1415 'menu_id' => '', 1416 'menu_class' => 'menu', 1417 'container' => 'div', 1418 'echo' => true, 1419 'link_before' => '', 1420 'link_after' => '', 1421 'before' => '<ul>', 1422 'after' => '</ul>', 1423 'item_spacing' => 'discard', 1424 'walker' => '', 1425 ); 1426 $args = wp_parse_args( $args, $defaults ); 1427 1428 if ( ! in_array( $args['item_spacing'], array( 'preserve', 'discard' ), true ) ) { 1429 // Invalid value, fall back to default. 1430 $args['item_spacing'] = $defaults['item_spacing']; 1431 } 1432 1433 if ( 'preserve' === $args['item_spacing'] ) { 1434 $t = "\t"; 1435 $n = "\n"; 1436 } else { 1437 $t = ''; 1438 $n = ''; 1439 } 1440 1441 /** 1442 * Filters the arguments used to generate a page-based menu. 1443 * 1444 * @since 2.7.0 1445 * 1446 * @see wp_page_menu() 1447 * 1448 * @param array $args An array of page menu arguments. See wp_page_menu() 1449 * for information on accepted arguments. 1450 */ 1451 $args = apply_filters( 'wp_page_menu_args', $args ); 1452 1453 $menu = ''; 1454 1455 $list_args = $args; 1456 1457 // Show Home in the menu. 1458 if ( ! empty( $args['show_home'] ) ) { 1459 if ( true === $args['show_home'] || '1' === $args['show_home'] || 1 === $args['show_home'] ) { 1460 $text = __( 'Home' ); 1461 } else { 1462 $text = $args['show_home']; 1463 } 1464 $class = ''; 1465 if ( is_front_page() && ! is_paged() ) { 1466 $class = 'class="current_page_item"'; 1467 } 1468 $menu .= '<li ' . $class . '><a href="' . esc_url( home_url( '/' ) ) . '">' . $args['link_before'] . $text . $args['link_after'] . '</a></li>'; 1469 // If the front page is a page, add it to the exclude list. 1470 if ( 'page' === get_option( 'show_on_front' ) ) { 1471 if ( ! empty( $list_args['exclude'] ) ) { 1472 $list_args['exclude'] .= ','; 1473 } else { 1474 $list_args['exclude'] = ''; 1475 } 1476 $list_args['exclude'] .= get_option( 'page_on_front' ); 1477 } 1478 } 1479 1480 $list_args['echo'] = false; 1481 $list_args['title_li'] = ''; 1482 $menu .= wp_list_pages( $list_args ); 1483 1484 $container = sanitize_text_field( $args['container'] ); 1485 1486 // Fallback in case `wp_nav_menu()` was called without a container. 1487 if ( empty( $container ) ) { 1488 $container = 'div'; 1489 } 1490 1491 if ( $menu ) { 1492 1493 // wp_nav_menu() doesn't set before and after. 1494 if ( isset( $args['fallback_cb'] ) && 1495 'wp_page_menu' === $args['fallback_cb'] && 1496 'ul' !== $container ) { 1497 $args['before'] = "<ul>{$n}"; 1498 $args['after'] = '</ul>'; 1499 } 1500 1501 $menu = $args['before'] . $menu . $args['after']; 1502 } 1503 1504 $attrs = ''; 1505 if ( ! empty( $args['menu_id'] ) ) { 1506 $attrs .= ' id="' . esc_attr( $args['menu_id'] ) . '"'; 1507 } 1508 1509 if ( ! empty( $args['menu_class'] ) ) { 1510 $attrs .= ' class="' . esc_attr( $args['menu_class'] ) . '"'; 1511 } 1512 1513 $menu = "<{$container}{$attrs}>" . $menu . "</{$container}>{$n}"; 1514 1515 /** 1516 * Filters the HTML output of a page-based menu. 1517 * 1518 * @since 2.7.0 1519 * 1520 * @see wp_page_menu() 1521 * 1522 * @param string $menu The HTML output. 1523 * @param array $args An array of arguments. See wp_page_menu() 1524 * for information on accepted arguments. 1525 */ 1526 $menu = apply_filters( 'wp_page_menu', $menu, $args ); 1527 1528 if ( $args['echo'] ) { 1529 echo $menu; 1530 } else { 1531 return $menu; 1532 } 1533 } 1534 1535 // 1536 // Page helpers. 1537 // 1538 1539 /** 1540 * Retrieves HTML list content for page list. 1541 * 1542 * @uses Walker_Page to create HTML list content. 1543 * @since 2.1.0 1544 * 1545 * @param array $pages 1546 * @param int $depth 1547 * @param int $current_page 1548 * @param array $args 1549 * @return string 1550 */ 1551 function walk_page_tree( $pages, $depth, $current_page, $args ) { 1552 if ( empty( $args['walker'] ) ) { 1553 $walker = new Walker_Page(); 1554 } else { 1555 /** 1556 * @var Walker $walker 1557 */ 1558 $walker = $args['walker']; 1559 } 1560 1561 foreach ( (array) $pages as $page ) { 1562 if ( $page->post_parent ) { 1563 $args['pages_with_children'][ $page->post_parent ] = true; 1564 } 1565 } 1566 1567 return $walker->walk( $pages, $depth, $args, $current_page ); 1568 } 1569 1570 /** 1571 * Retrieves HTML dropdown (select) content for page list. 1572 * 1573 * @since 2.1.0 1574 * @since 5.3.0 Formalized the existing `...$args` parameter by adding it 1575 * to the function signature. 1576 * 1577 * @uses Walker_PageDropdown to create HTML dropdown content. 1578 * @see Walker_PageDropdown::walk() for parameters and return description. 1579 * 1580 * @param mixed ...$args Elements array, maximum hierarchical depth and optional additional arguments. 1581 * @return string 1582 */ 1583 function walk_page_dropdown_tree( ...$args ) { 1584 if ( empty( $args[2]['walker'] ) ) { // The user's options are the third parameter. 1585 $walker = new Walker_PageDropdown(); 1586 } else { 1587 /** 1588 * @var Walker $walker 1589 */ 1590 $walker = $args[2]['walker']; 1591 } 1592 1593 return $walker->walk( ...$args ); 1594 } 1595 1596 // 1597 // Attachments. 1598 // 1599 1600 /** 1601 * Displays an attachment page link using an image or icon. 1602 * 1603 * @since 2.0.0 1604 * 1605 * @param int|WP_Post $post Optional. Post ID or post object. 1606 * @param bool $fullsize Optional. Whether to use full size. Default false. 1607 * @param bool $deprecated Deprecated. Not used. 1608 * @param bool $permalink Optional. Whether to include permalink. Default false. 1609 */ 1610 function the_attachment_link( $post = 0, $fullsize = false, $deprecated = false, $permalink = false ) { 1611 if ( ! empty( $deprecated ) ) { 1612 _deprecated_argument( __FUNCTION__, '2.5.0' ); 1613 } 1614 1615 if ( $fullsize ) { 1616 echo wp_get_attachment_link( $post, 'full', $permalink ); 1617 } else { 1618 echo wp_get_attachment_link( $post, 'thumbnail', $permalink ); 1619 } 1620 } 1621 1622 /** 1623 * Retrieves an attachment page link using an image or icon, if possible. 1624 * 1625 * @since 2.5.0 1626 * @since 4.4.0 The `$post` parameter can now accept either a post ID or `WP_Post` object. 1627 * 1628 * @param int|WP_Post $post Optional. Post ID or post object. 1629 * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array 1630 * of width and height values in pixels (in that order). Default 'thumbnail'. 1631 * @param bool $permalink Optional. Whether to add permalink to image. Default false. 1632 * @param bool $icon Optional. Whether the attachment is an icon. Default false. 1633 * @param string|false $text Optional. Link text to use. Activated by passing a string, false otherwise. 1634 * Default false. 1635 * @param array|string $attr Optional. Array or string of attributes. Default empty. 1636 * @return string HTML content. 1637 */ 1638 function wp_get_attachment_link( $post = 0, $size = 'thumbnail', $permalink = false, $icon = false, $text = false, $attr = '' ) { 1639 $_post = get_post( $post ); 1640 1641 if ( empty( $_post ) || ( 'attachment' !== $_post->post_type ) || ! wp_get_attachment_url( $_post->ID ) ) { 1642 return __( 'Missing Attachment' ); 1643 } 1644 1645 $url = wp_get_attachment_url( $_post->ID ); 1646 1647 if ( $permalink ) { 1648 $url = get_attachment_link( $_post->ID ); 1649 } 1650 1651 if ( $text ) { 1652 $link_text = $text; 1653 } elseif ( $size && 'none' !== $size ) { 1654 $link_text = wp_get_attachment_image( $_post->ID, $size, $icon, $attr ); 1655 } else { 1656 $link_text = ''; 1657 } 1658 1659 if ( '' === trim( $link_text ) ) { 1660 $link_text = $_post->post_title; 1661 } 1662 1663 if ( '' === trim( $link_text ) ) { 1664 $link_text = esc_html( pathinfo( get_attached_file( $_post->ID ), PATHINFO_FILENAME ) ); 1665 } 1666 1667 /** 1668 * Filters the list of attachment link attributes. 1669 * 1670 * @since 6.2.0 1671 * 1672 * @param array $attributes An array of attributes for the link markup, 1673 * keyed on the attribute name. 1674 * @param int $id Post ID. 1675 */ 1676 $attributes = apply_filters( 'wp_get_attachment_link_attributes', array( 'href' => $url ), $_post->ID ); 1677 1678 $link_attributes = ''; 1679 foreach ( $attributes as $name => $value ) { 1680 $value = 'href' === $name ? esc_url( $value ) : esc_attr( $value ); 1681 $link_attributes .= ' ' . esc_attr( $name ) . "='" . $value . "'"; 1682 } 1683 1684 $link_html = "<a$link_attributes>$link_text</a>"; 1685 1686 /** 1687 * Filters a retrieved attachment page link. 1688 * 1689 * @since 2.7.0 1690 * @since 5.1.0 Added the `$attr` parameter. 1691 * 1692 * @param string $link_html The page link HTML output. 1693 * @param int|WP_Post $post Post ID or object. Can be 0 for the current global post. 1694 * @param string|int[] $size Requested image size. Can be any registered image size name, or 1695 * an array of width and height values in pixels (in that order). 1696 * @param bool $permalink Whether to add permalink to image. Default false. 1697 * @param bool $icon Whether to include an icon. 1698 * @param string|false $text If string, will be link text. 1699 * @param array|string $attr Array or string of attributes. 1700 */ 1701 return apply_filters( 'wp_get_attachment_link', $link_html, $post, $size, $permalink, $icon, $text, $attr ); 1702 } 1703 1704 /** 1705 * Wraps attachment in paragraph tag before content. 1706 * 1707 * @since 2.0.0 1708 * 1709 * @param string $content 1710 * @return string 1711 */ 1712 function prepend_attachment( $content ) { 1713 $post = get_post(); 1714 1715 if ( empty( $post->post_type ) || 'attachment' !== $post->post_type ) { 1716 return $content; 1717 } 1718 1719 if ( wp_attachment_is( 'video', $post ) ) { 1720 $meta = wp_get_attachment_metadata( get_the_ID() ); 1721 $atts = array( 'src' => wp_get_attachment_url() ); 1722 if ( ! empty( $meta['width'] ) && ! empty( $meta['height'] ) ) { 1723 $atts['width'] = (int) $meta['width']; 1724 $atts['height'] = (int) $meta['height']; 1725 } 1726 if ( has_post_thumbnail() ) { 1727 $atts['poster'] = wp_get_attachment_url( get_post_thumbnail_id() ); 1728 } 1729 $p = wp_video_shortcode( $atts ); 1730 } elseif ( wp_attachment_is( 'audio', $post ) ) { 1731 $p = wp_audio_shortcode( array( 'src' => wp_get_attachment_url() ) ); 1732 } else { 1733 $p = '<p class="attachment">'; 1734 // Show the medium sized image representation of the attachment if available, and link to the raw file. 1735 $p .= wp_get_attachment_link( 0, 'medium', false ); 1736 $p .= '</p>'; 1737 } 1738 1739 /** 1740 * Filters the attachment markup to be prepended to the post content. 1741 * 1742 * @since 2.0.0 1743 * 1744 * @see prepend_attachment() 1745 * 1746 * @param string $p The attachment HTML output. 1747 */ 1748 $p = apply_filters( 'prepend_attachment', $p ); 1749 1750 return "$p\n$content"; 1751 } 1752 1753 // 1754 // Misc. 1755 // 1756 1757 /** 1758 * Retrieves protected post password form content. 1759 * 1760 * @since 1.0.0 1761 * 1762 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post. 1763 * @return string HTML content for password form for password protected post. 1764 */ 1765 function get_the_password_form( $post = 0 ) { 1766 $post = get_post( $post ); 1767 $label = 'pwbox-' . ( empty( $post->ID ) ? rand() : $post->ID ); 1768 $output = '<form action="' . esc_url( site_url( 'wp-login.php?action=postpass', 'login_post' ) ) . '" class="post-password-form" method="post"> 1769 <p>' . __( 'This content is password protected. To view it please enter your password below:' ) . '</p> 1770 <p><label for="' . $label . '">' . __( 'Password:' ) . ' <input name="post_password" id="' . $label . '" type="password" spellcheck="false" size="20" /></label> <input type="submit" name="Submit" value="' . esc_attr_x( 'Enter', 'post password form' ) . '" /></p></form> 1771 '; 1772 1773 /** 1774 * Filters the HTML output for the protected post password form. 1775 * 1776 * If modifying the password field, please note that the core database schema 1777 * limits the password field to 20 characters regardless of the value of the 1778 * size attribute in the form input. 1779 * 1780 * @since 2.7.0 1781 * @since 5.8.0 Added the `$post` parameter. 1782 * 1783 * @param string $output The password form HTML output. 1784 * @param WP_Post $post Post object. 1785 */ 1786 return apply_filters( 'the_password_form', $output, $post ); 1787 } 1788 1789 /** 1790 * Determines whether the current post uses a page template. 1791 * 1792 * This template tag allows you to determine if you are in a page template. 1793 * You can optionally provide a template filename or array of template filenames 1794 * and then the check will be specific to that template. 1795 * 1796 * For more information on this and similar theme functions, check out 1797 * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ 1798 * Conditional Tags} article in the Theme Developer Handbook. 1799 * 1800 * @since 2.5.0 1801 * @since 4.2.0 The `$template` parameter was changed to also accept an array of page templates. 1802 * @since 4.7.0 Now works with any post type, not just pages. 1803 * 1804 * @param string|string[] $template The specific template filename or array of templates to match. 1805 * @return bool True on success, false on failure. 1806 */ 1807 function is_page_template( $template = '' ) { 1808 if ( ! is_singular() ) { 1809 return false; 1810 } 1811 1812 $page_template = get_page_template_slug( get_queried_object_id() ); 1813 1814 if ( empty( $template ) ) { 1815 return (bool) $page_template; 1816 } 1817 1818 if ( $template == $page_template ) { 1819 return true; 1820 } 1821 1822 if ( is_array( $template ) ) { 1823 if ( ( in_array( 'default', $template, true ) && ! $page_template ) 1824 || in_array( $page_template, $template, true ) 1825 ) { 1826 return true; 1827 } 1828 } 1829 1830 return ( 'default' === $template && ! $page_template ); 1831 } 1832 1833 /** 1834 * Gets the specific template filename for a given post. 1835 * 1836 * @since 3.4.0 1837 * @since 4.7.0 Now works with any post type, not just pages. 1838 * 1839 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post. 1840 * @return string|false Page template filename. Returns an empty string when the default page template 1841 * is in use. Returns false if the post does not exist. 1842 */ 1843 function get_page_template_slug( $post = null ) { 1844 $post = get_post( $post ); 1845 1846 if ( ! $post ) { 1847 return false; 1848 } 1849 1850 $template = get_post_meta( $post->ID, '_wp_page_template', true ); 1851 1852 if ( ! $template || 'default' === $template ) { 1853 return ''; 1854 } 1855 1856 return $template; 1857 } 1858 1859 /** 1860 * Retrieves formatted date timestamp of a revision (linked to that revisions's page). 1861 * 1862 * @since 2.6.0 1863 * 1864 * @param int|object $revision Revision ID or revision object. 1865 * @param bool $link Optional. Whether to link to revision's page. Default true. 1866 * @return string|false i18n formatted datetimestamp or localized 'Current Revision'. 1867 */ 1868 function wp_post_revision_title( $revision, $link = true ) { 1869 $revision = get_post( $revision ); 1870 1871 if ( ! $revision ) { 1872 return $revision; 1873 } 1874 1875 if ( ! in_array( $revision->post_type, array( 'post', 'page', 'revision' ), true ) ) { 1876 return false; 1877 } 1878 1879 /* translators: Revision date format, see https://www.php.net/manual/datetime.format.php */ 1880 $datef = _x( 'F j, Y @ H:i:s', 'revision date format' ); 1881 /* translators: %s: Revision date. */ 1882 $autosavef = __( '%s [Autosave]' ); 1883 /* translators: %s: Revision date. */ 1884 $currentf = __( '%s [Current Revision]' ); 1885 1886 $date = date_i18n( $datef, strtotime( $revision->post_modified ) ); 1887 $edit_link = get_edit_post_link( $revision->ID ); 1888 if ( $link && current_user_can( 'edit_post', $revision->ID ) && $edit_link ) { 1889 $date = "<a href='$edit_link'>$date</a>"; 1890 } 1891 1892 if ( ! wp_is_post_revision( $revision ) ) { 1893 $date = sprintf( $currentf, $date ); 1894 } elseif ( wp_is_post_autosave( $revision ) ) { 1895 $date = sprintf( $autosavef, $date ); 1896 } 1897 1898 return $date; 1899 } 1900 1901 /** 1902 * Retrieves formatted date timestamp of a revision (linked to that revisions's page). 1903 * 1904 * @since 3.6.0 1905 * 1906 * @param int|object $revision Revision ID or revision object. 1907 * @param bool $link Optional. Whether to link to revision's page. Default true. 1908 * @return string|false gravatar, user, i18n formatted datetimestamp or localized 'Current Revision'. 1909 */ 1910 function wp_post_revision_title_expanded( $revision, $link = true ) { 1911 $revision = get_post( $revision ); 1912 1913 if ( ! $revision ) { 1914 return $revision; 1915 } 1916 1917 if ( ! in_array( $revision->post_type, array( 'post', 'page', 'revision' ), true ) ) { 1918 return false; 1919 } 1920 1921 $author = get_the_author_meta( 'display_name', $revision->post_author ); 1922 /* translators: Revision date format, see https://www.php.net/manual/datetime.format.php */ 1923 $datef = _x( 'F j, Y @ H:i:s', 'revision date format' ); 1924 1925 $gravatar = get_avatar( $revision->post_author, 24 ); 1926 1927 $date = date_i18n( $datef, strtotime( $revision->post_modified ) ); 1928 $edit_link = get_edit_post_link( $revision->ID ); 1929 if ( $link && current_user_can( 'edit_post', $revision->ID ) && $edit_link ) { 1930 $date = "<a href='$edit_link'>$date</a>"; 1931 } 1932 1933 $revision_date_author = sprintf( 1934 /* translators: Post revision title. 1: Author avatar, 2: Author name, 3: Time ago, 4: Date. */ 1935 __( '%1$s %2$s, %3$s ago (%4$s)' ), 1936 $gravatar, 1937 $author, 1938 human_time_diff( strtotime( $revision->post_modified_gmt ) ), 1939 $date 1940 ); 1941 1942 /* translators: %s: Revision date with author avatar. */ 1943 $autosavef = __( '%s [Autosave]' ); 1944 /* translators: %s: Revision date with author avatar. */ 1945 $currentf = __( '%s [Current Revision]' ); 1946 1947 if ( ! wp_is_post_revision( $revision ) ) { 1948 $revision_date_author = sprintf( $currentf, $revision_date_author ); 1949 } elseif ( wp_is_post_autosave( $revision ) ) { 1950 $revision_date_author = sprintf( $autosavef, $revision_date_author ); 1951 } 1952 1953 /** 1954 * Filters the formatted author and date for a revision. 1955 * 1956 * @since 4.4.0 1957 * 1958 * @param string $revision_date_author The formatted string. 1959 * @param WP_Post $revision The revision object. 1960 * @param bool $link Whether to link to the revisions page, as passed into 1961 * wp_post_revision_title_expanded(). 1962 */ 1963 return apply_filters( 'wp_post_revision_title_expanded', $revision_date_author, $revision, $link ); 1964 } 1965 1966 /** 1967 * Displays a list of a post's revisions. 1968 * 1969 * Can output either a UL with edit links or a TABLE with diff interface, and 1970 * restore action links. 1971 * 1972 * @since 2.6.0 1973 * 1974 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post. 1975 * @param string $type 'all' (default), 'revision' or 'autosave' 1976 */ 1977 function wp_list_post_revisions( $post = 0, $type = 'all' ) { 1978 $post = get_post( $post ); 1979 1980 if ( ! $post ) { 1981 return; 1982 } 1983 1984 // $args array with (parent, format, right, left, type) deprecated since 3.6. 1985 if ( is_array( $type ) ) { 1986 $type = ! empty( $type['type'] ) ? $type['type'] : $type; 1987 _deprecated_argument( __FUNCTION__, '3.6.0' ); 1988 } 1989 1990 $revisions = wp_get_post_revisions( $post->ID ); 1991 1992 if ( ! $revisions ) { 1993 return; 1994 } 1995 1996 $rows = ''; 1997 foreach ( $revisions as $revision ) { 1998 if ( ! current_user_can( 'read_post', $revision->ID ) ) { 1999 continue; 2000 } 2001 2002 $is_autosave = wp_is_post_autosave( $revision ); 2003 if ( ( 'revision' === $type && $is_autosave ) || ( 'autosave' === $type && ! $is_autosave ) ) { 2004 continue; 2005 } 2006 2007 $rows .= "\t<li>" . wp_post_revision_title_expanded( $revision ) . "</li>\n"; 2008 } 2009 2010 echo "<div class='hide-if-js'><p>" . __( 'JavaScript must be enabled to use this feature.' ) . "</p></div>\n"; 2011 2012 echo "<ul class='post-revisions hide-if-no-js'>\n"; 2013 echo $rows; 2014 echo '</ul>'; 2015 } 2016 2017 /** 2018 * Retrieves the parent post object for the given post. 2019 * 2020 * @since 5.7.0 2021 * 2022 * @param int|WP_Post|null $post Optional. Post ID or WP_Post object. Default is global $post. 2023 * @return WP_Post|null Parent post object, or null if there isn't one. 2024 */ 2025 function get_post_parent( $post = null ) { 2026 $wp_post = get_post( $post ); 2027 return ! empty( $wp_post->post_parent ) ? get_post( $wp_post->post_parent ) : null; 2028 } 2029 2030 /** 2031 * Returns whether the given post has a parent post. 2032 * 2033 * @since 5.7.0 2034 * 2035 * @param int|WP_Post|null $post Optional. Post ID or WP_Post object. Default is global $post. 2036 * @return bool Whether the post has a parent post. 2037 */ 2038 function has_post_parent( $post = null ) { 2039 return (bool) get_post_parent( $post ); 2040 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated : Tue Mar 19 08:20:01 2024 | Cross-referenced by PHPXref |