Skip to content

Commit

Permalink
Fix browser titles (#2672)
Browse files Browse the repository at this point in the history
  • Loading branch information
renintw authored Jul 18, 2024
1 parent ef8e548 commit fea1e5b
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
1 change: 1 addition & 0 deletions wp-content/themes/pub/wporg-learn-2024/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
require_once __DIR__ . '/inc/block-config.php';
require_once __DIR__ . '/inc/block-hooks.php';
require_once __DIR__ . '/inc/query.php';
require_once __DIR__ . '/inc/head.php';

/**
* Actions and filters.
Expand Down
59 changes: 59 additions & 0 deletions wp-content/themes/pub/wporg-learn-2024/inc/head.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
/**
* HTML head markup and customizations.
*/

namespace WordPressdotorg\Theme\Learn_2024\Head;

add_action( 'init', __NAMESPACE__ . '\init' );

/**
* Handles adding/removing hooks as needed.
*/
function init() {
add_filter( 'document_title_parts', __NAMESPACE__ . '\document_title' );
add_filter( 'document_title_separator', __NAMESPACE__ . '\document_title_separator' );
}

/**
* Filters document title to add context based on what is being viewed.
*
* @param array $parts The document title parts.
* @return array The document title parts.
*/
function document_title( $parts ) {
global $wp_query;

$parts['site'] = __( 'Learn.WordPress.org', 'wporg-learn' );
$post_type = get_query_var( 'post_type' );
$sep = '';

if ( is_singular() ) {
// Add post type to title if it's a parsed item.
if ( get_post_type_object( $post_type ) ) {
$parts['title'] .= " $sep " . get_post_type_object( $post_type )->labels->singular_name;
}
}

// If results are paged and the max number of pages is known.
if ( is_paged() && $wp_query->max_num_pages ) {
$parts['page'] = sprintf(
// translators: 1: current page number, 2: total number of pages
__( 'Page %1$s of %2$s', 'wporg-learn' ),
get_query_var( 'paged' ),
$wp_query->max_num_pages
);
}

return $parts;
}

/**
* Customizes the document title separator.
*
* @param string $separator Current document title separator.
* @return string
*/
function document_title_separator( $separator ) {
return '|';
}

0 comments on commit fea1e5b

Please sign in to comment.