WordPress 移除归档页面的“分类:”,即自定义the_archive_title输出

发布时间:2023-10-20 19:40 最后更新:2023-10-20 19:41 浏览量:共 36 次浏览

WordPress 移除归档页面的“分类:”,即自定义the_archive_title输出 – WordPress大学 (wpdaxue.com)

今天有朋友问了一个问题,如何移除归档页面分类或标签名称前面的“分类:”和“标签:”,如下图:

图片说明:WordPress 移除归档页面的“分类:”,即自定义the_archive_title输出-1

首先,我们要先了解这两个字是通过什么函数调用出来的,在比较正规的主题中,一般会用以下代码在归档页面输入标题:

  1. <?php the_archive_title( '<h1 class="page-title">', '</h1>' ); ?>

而这个 the_archive_title() 函数的代码为:

  1. function the_archive_title( $before = '', $after = '' ) {
  2. $title = get_the_archive_title();
  3. if ( ! empty( $title ) ) {
  4. echo $before . $title . $after;
  5. }
  6. }

可以看到,调用的是  get_the_archive_title() 的内容,我们再来看看这个  get_the_archive_title() 的代码:

  1. function get_the_archive_title() {
  2. if ( is_category() ) {
  3. /* translators: Category archive title. %s: Category name */
  4. $title = sprintf( __( 'Category: %s' ), single_cat_title( '', false ) );
  5. } elseif ( is_tag() ) {
  6. /* translators: Tag archive title. %s: Tag name */
  7. $title = sprintf( __( 'Tag: %s' ), single_tag_title( '', false ) );
  8. } elseif ( is_author() ) {
  9. /* translators: Author archive title. %s: Author name */
  10. $title = sprintf( __( 'Author: %s' ), '<span class="vcard">' . get_the_author() . '</span>' );
  11. } elseif ( is_year() ) {
  12. /* translators: Yearly archive title. %s: Year */
  13. $title = sprintf( __( 'Year: %s' ), get_the_date( _x( 'Y', 'yearly archives date format' ) ) );
  14. } elseif ( is_month() ) {
  15. /* translators: Monthly archive title. %s: Month name and year */
  16. $title = sprintf( __( 'Month: %s' ), get_the_date( _x( 'F Y', 'monthly archives date format' ) ) );
  17. } elseif ( is_day() ) {
  18. /* translators: Daily archive title. %s: Date */
  19. $title = sprintf( __( 'Day: %s' ), get_the_date( _x( 'F j, Y', 'daily archives date format' ) ) );
  20. } elseif ( is_tax( 'post_format' ) ) {
  21. if ( is_tax( 'post_format', 'post-format-aside' ) ) {
  22. $title = _x( 'Asides', 'post format archive title' );
  23. } elseif ( is_tax( 'post_format', 'post-format-gallery' ) ) {
  24. $title = _x( 'Galleries', 'post format archive title' );
  25. } elseif ( is_tax( 'post_format', 'post-format-image' ) ) {
  26. $title = _x( 'Images', 'post format archive title' );
  27. } elseif ( is_tax( 'post_format', 'post-format-video' ) ) {
  28. $title = _x( 'Videos', 'post format archive title' );
  29. } elseif ( is_tax( 'post_format', 'post-format-quote' ) ) {
  30. $title = _x( 'Quotes', 'post format archive title' );
  31. } elseif ( is_tax( 'post_format', 'post-format-link' ) ) {
  32. $title = _x( 'Links', 'post format archive title' );
  33. } elseif ( is_tax( 'post_format', 'post-format-status' ) ) {
  34. $title = _x( 'Statuses', 'post format archive title' );
  35. } elseif ( is_tax( 'post_format', 'post-format-audio' ) ) {
  36. $title = _x( 'Audio', 'post format archive title' );
  37. } elseif ( is_tax( 'post_format', 'post-format-chat' ) ) {
  38. $title = _x( 'Chats', 'post format archive title' );
  39. }
  40. } elseif ( is_post_type_archive() ) {
  41. /* translators: Post type archive title. %s: Post type name */
  42. $title = sprintf( __( 'Archives: %s' ), post_type_archive_title( '', false ) );
  43. } elseif ( is_tax() ) {
  44. $tax = get_taxonomy( get_queried_object()->taxonomy );
  45. /* translators: Taxonomy term archive title. 1: Taxonomy singular name, 2: Current taxonomy term */
  46. $title = sprintf( __( '%1$s: %2$s' ), $tax->labels->singular_name, single_term_title( '', false ) );
  47. } else {
  48. $title = __( 'Archives' );
  49. }
  50. /**
  51. * Filters the archive title.
  52. *
  53. * @since 4.1.0
  54. *
  55. * @param string $title Archive title to be displayed.
  56. */
  57. return apply_filters( 'get_the_archive_title', $title );
  58. }

好长一段代码,注意看倒数第二行代码为:

  1. return apply_filters( 'get_the_archive_title', $title );

此处应用了一个过滤钩子,也就是我们可以通过这个钩子修改 get_the_archive_title() 的内容,从而实现修改 the_archive_title() 输出的内容。

要实现刚才我们说的去掉归档页面的 “分类:”和“标签:”,可以使用下面的代码:

function my_theme_archive_title( $title ) {
    if ( is_category() ) {
        $title = single_cat_title( '', false );
    } elseif ( is_tag() ) {
        $title = single_tag_title( '', false );
    } elseif ( is_author() ) {
        $title = '<span class="vcard">' . get_the_author() . '</span>';
    } elseif ( is_post_type_archive() ) {
        $title = post_type_archive_title( '', false );
    } elseif ( is_tax() ) {
        $title = single_term_title( '', false );
    }
  
    return $title;
}
 
add_filter( 'get_the_archive_title', 'my_theme_archive_title' );

将该代码添加到当前使用的主题的 functions.php 文件即可。

相关文章

调所有前端VUE,标题四个字,超过自动调行,字和框竖向自动居中
【Simply Static生成静态失败】宝塔防火墙拦截了服务器本机访问自身域名 / IP 的环回请求
zhong zhuan
宝塔面板中如何修改网站顺序
Ubuntun和debian 系统升级
【教程】Simply Static配置详情