WordPress教程

WordPress 移除归档页面的“分类:”

阿里云

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

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

  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.  
  4.     if ( ! empty( $title ) ) {
  5.         echo $before . $title . $after;
  6.     }
  7. }

可以看到,调用的是 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.     /**
  52.      * Filters the archive title.
  53.      *
  54.      * @since 4.1.0
  55.      *
  56.      * @param string $title Archive title to be displayed.
  57.      */
  58.     return apply_filters( 'get_the_archive_title', $title );

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

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

此处应用了一个过滤钩子,也就是我们可以通过这个钩子修改 get_the_archive_title() 的内容,从而实现修改 the_archive_title() 输出的内容。要实现刚才我们说的去掉归档页面的 “分类:”和“标签:”,可以使用下面的代码:

  1. function my_theme_archive_title( $title ) {
  2.     if ( is_category() ) {
  3.         $title = single_cat_title( '', false );
  4.     } elseif ( is_tag() ) {
  5.         $title = single_tag_title( '', false );
  6.     } elseif ( is_author() ) {
  7.         $title = '<span class="vcard">' . get_the_author() . '</span>';
  8.     } elseif ( is_post_type_archive() ) {
  9.         $title = post_type_archive_title( '', false );
  10.     } elseif ( is_tax() ) {
  11.         $title = single_term_title( '', false );
  12.     }
  13.  
  14.     return $title;
  15. }
  16.  
  17. add_filter( 'get_the_archive_title', 'my_theme_archive_title' );

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

WordPress 移除归档页面的“分类:”

已有 478 人购买
查看演示升级 VIP立刻购买

收藏
(0)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!