WordPress.org

Make WordPress Core

Changeset 30223


Ignore:
Timestamp:
11/04/2014 12:35:00 AM (5 years ago)
Author:
johnbillion
Message:

Introduce some new template functions for archive titles and descriptions:

  • get_the_archive_title() and the_archive_title() for returning/displaying the title of the current term, date, post type, post format, or author archive.
  • get_the_archive_description() and the_archive_description() for returning/displaying the description associated with the current term archive.

Fixes #21995
Props obenland, DrewAPicture

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/general-template.php

    r30133 r30223  
    11091109        return $result;
    11101110    echo $result;
     1111}
     1112
     1113/**
     1114 * Display the archive title based on the queried object.
     1115 *
     1116 * @since 4.1.0
     1117 *
     1118 * @param string $before Optional. Content to prepend to the title.
     1119 * @param string $after  Optional. Content to append to the title.
     1120 */
     1121function the_archive_title( $before = '', $after = '' ) {
     1122    $title = get_the_archive_title();
     1123
     1124    if ( ! empty( $title ) ) {
     1125        echo $before . $title . $after;
     1126    }
     1127}
     1128
     1129/**
     1130 * Retrieve the archive title based on the queried object.
     1131 *
     1132 * @since 4.1.0
     1133 *
     1134 * @return string Archive title.
     1135 */
     1136function get_the_archive_title() {
     1137    if ( is_category() ) {
     1138        $title = sprintf( __( 'Category: %s' ), single_cat_title( '', false ) );
     1139    } elseif ( is_tag() ) {
     1140        $title = sprintf( __( 'Tag: %s' ), single_tag_title( '', false ) );
     1141    } elseif ( is_author() ) {
     1142        $title = sprintf( __( 'Author: %s' ), '<span class="vcard">' . get_the_author() . '</span>' );
     1143    } elseif ( is_year() ) {
     1144        $title = sprintf( __( 'Year: %s' ), get_the_date( _x( 'Y', 'yearly archives date format' ) ) );
     1145    } elseif ( is_month() ) {
     1146        $title = sprintf( __( 'Month: %s' ), get_the_date( _x( 'F Y', 'monthly archives date format' ) ) );
     1147    } elseif ( is_day() ) {
     1148        $title = sprintf( __( 'Day: %s' ), get_the_date( _x( 'F j, Y', 'daily archives date format' ) ) );
     1149    } elseif ( is_tax( 'post_format', 'post-format-aside' ) ) {
     1150        $title = _x( 'Asides', 'post format archive title' );
     1151    } elseif ( is_tax( 'post_format', 'post-format-gallery' ) ) {
     1152        $title = _x( 'Galleries', 'post format archive title' );
     1153    } elseif ( is_tax( 'post_format', 'post-format-image' ) ) {
     1154        $title = _x( 'Images', 'post format archive title' );
     1155    } elseif ( is_tax( 'post_format', 'post-format-video' ) ) {
     1156        $title = _x( 'Videos', 'post format archive title' );
     1157    } elseif ( is_tax( 'post_format', 'post-format-quote' ) ) {
     1158        $title = _x( 'Quotes', 'post format archive title' );
     1159    } elseif ( is_tax( 'post_format', 'post-format-link' ) ) {
     1160        $title = _x( 'Links', 'post format archive title' );
     1161    } elseif ( is_tax( 'post_format', 'post-format-status' ) ) {
     1162        $title = _x( 'Statuses', 'post format archive title' );
     1163    } elseif ( is_tax( 'post_format', 'post-format-audio' ) ) {
     1164        $title = _x( 'Audio', 'post format archive title' );
     1165    } elseif ( is_tax( 'post_format', 'post-format-chat' ) ) {
     1166        $title = _x( 'Chats', 'post format archive title' );
     1167    } elseif ( is_post_type_archive() ) {
     1168        $title = sprintf( __( 'Archives: %s' ), post_type_archive_title( '', false ) );
     1169    } elseif ( is_tax() ) {
     1170        $tax = get_taxonomy( get_queried_object()->taxonomy );
     1171        /* translators: 1: Taxonomy singular name, 2: Current taxonomy term */
     1172        $title = sprintf( __( '%1$s: %2$s' ), $tax->labels->singular_name, single_term_title( '', false ) );
     1173    } else {
     1174        $title = __( 'Archives' );
     1175    }
     1176
     1177    /**
     1178     * Filter the archive title.
     1179     *
     1180     * @since 4.1.0
     1181     *
     1182     * @param string $title Archive title to be displayed.
     1183     */
     1184    return apply_filters( 'get_the_archive_title', $title );
     1185}
     1186
     1187/**
     1188 * Display category, tag, or term description.
     1189 *
     1190 * @since 4.1.0
     1191 *
     1192 * @param string $before Optional. Content to prepend to the description.
     1193 * @param string $after  Optional. Content to append to the description.
     1194 */
     1195function the_archive_description( $before = '', $after = '' ) {
     1196    $description = get_the_archive_description();
     1197
     1198    if ( ! empty( $description ) ) {
     1199        echo $before . $description . $after;
     1200    }
     1201}
     1202
     1203/**
     1204 * Retrieve category, tag, or term description.
     1205 *
     1206 * @since 4.1.0
     1207 *
     1208 * @return string Archive description.
     1209 */
     1210function get_the_archive_description() {
     1211
     1212    /**
     1213     * Filter the archive description.
     1214     *
     1215     * @since 4.1.0
     1216     *
     1217     * @param string $description Archive description to be displayed.
     1218     */
     1219    return apply_filters( 'get_the_archive_description', term_description() );
    11111220}
    11121221
Note: See TracChangeset for help on using the changeset viewer.