WordPress基础教程

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

发布时间:2019-04-22 12:46:18

本文作者:

最后更新时间为 2019年4月22日

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

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

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

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

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

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

小兽wordpress凭借多年的wordpress企业主题制作经验,坚持以“为用户而生的wordpress主题”为宗旨,累计为10万多家客户提供品质wordpress建站服务,得到了客户的一致好评。我们一直用心对待每一个客户,我们坚信:“善待客户,将会成为终身客户”。小兽wordpress能坚持多年,是因为我们一直诚信。我们明码标价(wordpress做网站需要多少钱),从不忽悠任何客户,我们的报价宗旨:“拒绝暴利,只保留合理的利润”。如果您有网站建设、网站改版、网站维护等方面的需求,请立即咨询右侧在线客服或拨打咨询热线:18907337671,我们会详细为你一一解答你心中的疑难。

相关文章

为什么选择小兽WordPress

哪怕不计成本的时间投入,也绝不辜负任何一个客户。
成就客户成就自我的同时,把这个世界变得再美好一点。

我们的故事

小兽WordPress是2011年开始接触互联网,在这期间用WordPress搭建了一个博客叫小兽WordPress,专门学习WordPress建站。

我们的使命

我们希望用WordPress这款优秀的建站程序帮助每个人搭建一个属于自己的网站,高效率低成本做好互联网营销。

我们的文化

我们专注于把客户的事情做好并且做对,这意味着我们在做决定时会考虑到结果,最重要的是,我们力求真实。

我们的未来

我们将在不久的未来成为国内最好的WordPress工作室,小兽WordPress希望和每一个客户成为朋友,一起见证这个美好未来。

为什么选择小兽WordPress

告诉我你是谁,为你量身定做建站方案

我们深知不同的人做网站的阶段不一样,有的是新手,有的是老手。
我们清楚不同的人做网站的目的不一样,有的是展示,有的是营销。

如何让线上业务更上一层楼

还没有WordPress网站

还没有WordPress网站

不管你从事什么行业,WordPress都会为你提供一个专业的主题模板。在WordPress市场上有成千上万的免费主题,适合很多中小企业。

查看所有模板
已经有WordPress网站

已经有WordPress网站

小兽WordPress诚邀你一起学习WordPress,愿与各方携手升级改善您的WordPress网站,一起交流网站加速,网站优化等问题。

马上交个朋友
微信联系
chat 扫码联系
模板建站
挑选模板
网站定制
免费诊断
咨询热线
咨询热线

189-0733-7671

返回顶部