Make WordPress Core

Changeset 48294


Ignore:
Timestamp:
07/04/2020 02:43:32 PM (4 years ago)
Author:
ocean90
Message:

Themes: Introduce get_the_archive_title_prefix filter for changing the prefix used for archive titles.

In get_the_archive_title() split the internal $title variable into $title and $prefix. By using the new get_the_archive_title_prefix filter the prefix can now wrapped with custom elements or removed completely by using

add_filter( 'get_the_archive_title_prefix', '__return_empty_string' );

Also, wrap the title part with a span element and pass the original title and prefix to the existing get_the_archive_title filter, allowing further customization to the archive titles.

Props Kaira, milindmore22, shireling, grapplerulrich, audrasjb, desrosj, Confridin, ramiy, ocean90.
Fixes #31237.
Fixes #38545.

File:
1 edited

Legend:

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

    r48283 r48294  
    16051605 */
    16061606function get_the_archive_title() {
    1607     $title = __( 'Archives' );
     1607    $title  = __( 'Archives' );
     1608    $prefix = '';
    16081609
    16091610    if ( is_category() ) {
    1610         /* translators: Category archive title. %s: Category name. */
    1611         $title = sprintf( __( 'Category: %s' ), single_cat_title( '', false ) );
     1611        $title  = single_cat_title( '', false );
     1612        $prefix = _x( 'Category:', 'category archive title prefix' );
    16121613    } elseif ( is_tag() ) {
    1613         /* translators: Tag archive title. %s: Tag name. */
    1614         $title = sprintf( __( 'Tag: %s' ), single_tag_title( '', false ) );
     1614        $title  = single_tag_title( '', false );
     1615        $prefix = _x( 'Tag:', 'tag archive title prefix' );
    16151616    } elseif ( is_author() ) {
    1616         /* translators: Author archive title. %s: Author name. */
    1617         $title = sprintf( __( 'Author: %s' ), '<span class="vcard">' . get_the_author() . '</span>' );
     1617        $title  = get_the_author();
     1618        $prefix = _x( 'Author:', 'author archive title prefix' );
    16181619    } elseif ( is_year() ) {
    1619         /* translators: Yearly archive title. %s: Year. */
    1620         $title = sprintf( __( 'Year: %s' ), get_the_date( _x( 'Y', 'yearly archives date format' ) ) );
     1620        $title  = get_the_date( _x( 'Y', 'yearly archives date format' ) );
     1621        $prefix = _x( 'Year:', 'date archive title prefix' );
    16211622    } elseif ( is_month() ) {
    1622         /* translators: Monthly archive title. %s: Month name and year. */
    1623         $title = sprintf( __( 'Month: %s' ), get_the_date( _x( 'F Y', 'monthly archives date format' ) ) );
     1623        $title  = get_the_date( _x( 'F Y', 'monthly archives date format' ) );
     1624        $prefix = _x( 'Month:', 'date archive title prefix' );
    16241625    } elseif ( is_day() ) {
    1625         /* translators: Daily archive title. %s: Date. */
    1626         $title = sprintf( __( 'Day: %s' ), get_the_date( _x( 'F j, Y', 'daily archives date format' ) ) );
     1626        $title  = get_the_date( _x( 'F j, Y', 'daily archives date format' ) );
     1627        $prefix = _x( 'Day:', 'date archive title prefix' );
    16271628    } elseif ( is_tax( 'post_format' ) ) {
    16281629        if ( is_tax( 'post_format', 'post-format-aside' ) ) {
     
    16461647        }
    16471648    } elseif ( is_post_type_archive() ) {
    1648         /* translators: Post type archive title. %s: Post type name. */
    1649         $title = sprintf( __( 'Archives: %s' ), post_type_archive_title( '', false ) );
     1649        $title  = post_type_archive_title( '', false );
     1650        $prefix = _x( 'Archives:', 'post type archive title prefix' );
    16501651    } elseif ( is_tax() ) {
    16511652        $queried_object = get_queried_object();
    16521653        if ( $queried_object ) {
    1653             $tax = get_taxonomy( $queried_object->taxonomy );
    1654             /* translators: Taxonomy term archive title. 1: Taxonomy singular name, 2: Current taxonomy term. */
    1655             $title = sprintf( __( '%1$s: %2$s' ), $tax->labels->singular_name, single_term_title( '', false ) );
    1656         }
     1654            $tax    = get_taxonomy( $queried_object->taxonomy );
     1655            $title  = single_term_title( '', false );
     1656            $prefix = sprintf(
     1657                /* translators: %s: Taxonomy singular name. */
     1658                _x( '%s:', 'taxonomy term archive title prefix' ),
     1659                $tax->labels->singular_name
     1660            );
     1661        }
     1662    }
     1663
     1664    $original_title = $title;
     1665
     1666    /**
     1667     * Filters the archive title prefix.
     1668     *
     1669     * @since 5.5.0
     1670     *
     1671     * @param string $prefix Archive title prefix.
     1672     */
     1673    $prefix = apply_filters( 'get_the_archive_title_prefix', $prefix );
     1674    if ( $prefix ) {
     1675        $title = sprintf(
     1676            /* translators: 1: Title prefix. 2: Title. */
     1677            _x( '%1$s %2$s', 'archive title' ),
     1678            $prefix,
     1679            '<span>' . $title . '</span>'
     1680        );
    16571681    }
    16581682
     
    16611685     *
    16621686     * @since 4.1.0
    1663      *
    1664      * @param string $title Archive title to be displayed.
    1665      */
    1666     return apply_filters( 'get_the_archive_title', $title );
     1687     * @since 5.5.0 Added the `$prefix` and `$original_title` parameters.
     1688     *
     1689     * @param string $title          Archive title to be displayed.
     1690     * @param string $original_title Archive title without prefix.
     1691     * @param string $prefix         Archive title prefix.
     1692     */
     1693    return apply_filters( 'get_the_archive_title', $title, $original_title, $prefix );
    16671694}
    16681695
Note: See TracChangeset for help on using the changeset viewer.