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

WordPress 移除归档页面的“分类:”,第1张

概述今天有朋友问了一个问题,如何移除归档页面分类或标签名称前面的“分类:”和“标签:”,如下图:首先,我们要先了解这两个字是通过什么函数调用出来的,

下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。内存溢出小编现在分享给大家,也给大家做个参考。

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



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

<?PHP the_archive_Title( '<h1 >','</h1>' ); ?>

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

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() 的代码:

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 >' . 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-image' ) ) {

$Title = _x( 'Images','post-format-vIDeo' ) ) {

$Title = _x( 'VIDeos','post-format-quote' ) ) {

$Title = _x( 'Quotes','post-format-link' ) ) {

$Title = _x( 'links','post-format-status' ) ) {

$Title = _x( 'Statuses','post-format-audio' ) ) {

$Title = _x( 'Audio','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 );

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

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 >' . 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 文件即可。

以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

总结

以上是内存溢出为你收集整理的WordPress 移除归档页面的“分类:”全部内容,希望文章能够帮你解决WordPress 移除归档页面的“分类:”所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zz/1007872.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存