如何自定义the_archive_title()?
-
-
将您的解决方案作为单独的答案发布.如果愿意,您也可以不接受我的,并接受自己的:-)Post your solution as a separate answer. You can also unaccept mine and accept your own if you wish :-)
- 0
- 2015-01-27
- Pieter Goosen
-
糟糕,我以为我删除了此修改.您的方法最终变得更好,所以我打算删除我的方法.我现在就做.Oops, I thought I deleted this edit. Your method ended up being better, so I meant to delete mine. I'll do that now.
- 0
- 2015-01-27
- fildred13
-
6 个回答
- 投票数
-
- 2015-01-25
如果您查看
get_the_archive_title()
的源代码,则您将会看到提供了一个名为get_the_archive_title
的过滤器,您可以通过该过滤器过滤函数的输出.您可以使用以下内容更改类别页面上的输出
add_filter( 'get_the_archive_title', function ( $title ) { if( is_category() ) { $title = single_cat_title( '', false ); } return $title; });
If you look at the source code of
get_the_archive_title()
, you will see that there is a filter supplied, calledget_the_archive_title
, through which you can filter the output from the function.You can use the following to change the output on a category page
add_filter( 'get_the_archive_title', function ( $title ) { if( is_category() ) { $title = single_cat_title( '', false ); } return $title; });
-
这在标题为" Archive:Books"(其中" Books"是自定义帖子类型)的存档页面上不起作用.如果类型是* category *或*tag *,`single_cat_title()`函数仅返回页面标题.您可以修改答案以包括适用于所有内容类型的解决方案吗?This doesn't work on an archive page with a title of `Archive: Books` (where `Books` is a custom post type). The `single_cat_title()` function only returns a page title if the type is a *category* or *tag*. Can you amend your answer to include a solution that works with all content types?
- 6
- 2015-09-25
- Quinn Comendant
-
@QuinnComendant查看源代码,第1239和1240行:-).如果您有问题,只需问一个特定于您问题的新问题@QuinnComendant Look at the source code, lines 1239 and 1240 :-). If you have an issue, simply ask a new question specific to your question
- 0
- 2015-09-25
- Pieter Goosen
-
正确,您必须为他们想要不同标题的每种分类法或帖子类型返回一个替代标题.我已经发布了一个答案,其中排除了所有类型的前缀.Right, one must return an alternate title for each taxonomy or post type they want different titles for. I've posted an answer that excludes prefixes from all types.
- 1
- 2015-09-27
- Quinn Comendant
-
- 2015-09-27
可接受的答案可以从类别档案标题中删除
Category:
前缀,但不能从其他分类法或帖子类型中删除.要排除其他前缀,有两个选项:-
为原始
get_the_archive_title()
函数中使用的所有变体重建标题:// Return an alternate title, without prefix, for every type used in the get_the_archive_title(). add_filter('get_the_archive_title', function ($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_year() ) { $title = get_the_date( _x( 'Y', 'yearly archives date format' ) ); } elseif ( is_month() ) { $title = get_the_date( _x( 'F Y', 'monthly archives date format' ) ); } elseif ( is_day() ) { $title = 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() ) { $title = post_type_archive_title( '', false ); } elseif ( is_tax() ) { $title = single_term_title( '', false ); } else { $title = __( 'Archives' ); } return $title; });
-
或者,简单地剥离任何看起来像标题前缀的内容(这可能会更改包含单词和冒号的实际标题):
// Simply remove anything that looks like an archive title prefix ("Archive:", "Foo:", "Bar:"). add_filter('get_the_archive_title', function ($title) { return preg_replace('/^\w+: /', '', $title); });
The accepted answer works to remove the
Category:
prefix from category archive titles, but not other taxonomy or post types. To exclude other prefixes, there are two options:Rebuild the title for all the variants used in the original
get_the_archive_title()
function:// Return an alternate title, without prefix, for every type used in the get_the_archive_title(). add_filter('get_the_archive_title', function ($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_year() ) { $title = get_the_date( _x( 'Y', 'yearly archives date format' ) ); } elseif ( is_month() ) { $title = get_the_date( _x( 'F Y', 'monthly archives date format' ) ); } elseif ( is_day() ) { $title = 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() ) { $title = post_type_archive_title( '', false ); } elseif ( is_tax() ) { $title = single_term_title( '', false ); } else { $title = __( 'Archives' ); } return $title; });
Or, simply strip anything that looks like a title prefix (which may alter actual titles which contain a word followed by the colon character):
// Simply remove anything that looks like an archive title prefix ("Archive:", "Foo:", "Bar:"). add_filter('get_the_archive_title', function ($title) { return preg_replace('/^\w+: /', '', $title); });
-
对于标题包含"链接类型:电视"之类的标题,正则表达式必须为"/^(\ w| \ s)+:/"For titles containing spaces like `link type: tv` the regex must be `/^(\w|\s)+: /`
- 0
- 2020-04-19
- aitor
-
-
这在标题为"存档:书籍"(其中"书籍"是自定义帖子类型)的存档页面上不起作用.This doesn't work on an archive page with a title of Archive: Books (where Books is a custom post type).
- 0
- 2017-04-26
- That Brazilian Guy
-
@ThatBrazilianGuy [`single_cat_title`](https://developer.wordpress.org/reference/functions/single_cat_title/)在内部使用`single_term_title`,它也适用于分类法.确保您的主题没有定义以下模板之一:"taxonomy-book.php"或"taxonomy.php",因为它们优先于" archive.php".另外,请考虑测试其他答案中的任何一种方法.这个答案已经很老了,在我发布它的时候还不错,但是我不再为WordPress开发.@ThatBrazilianGuy [`single_cat_title`](https://developer.wordpress.org/reference/functions/single_cat_title/) uses `single_term_title` internally, which works on taxonomies as well. Make sure your theme doesn't have one of the following templates defined: `taxonomy-book.php` or `taxonomy.php`, because they have precendence over `archive.php`. Also, consider testing any of the methods in the other answers. This answer is quite old, it was working fine at the time I posted it, but I'm not developing for WordPress anymore.
- 0
- 2017-04-26
- tao
-
-
- 2015-06-25
另一个选择是:
<?php echo str_replace('Brand: ','',get_the_archive_title()); ?>
替换品牌:使用您要删除的任何文字.
值得研究一下get_the_archive_title()和the_archive_title()之间的区别 the_archive_title()返回一个数组 get_the_archive_title()返回一个字符串
Another option is:
<?php echo str_replace('Brand: ','',get_the_archive_title()); ?>
Replace Brand: with whatever text you are wanting to get rid of.
Its worth looking into the difference between get_the_archive_title() and the_archive_title() the_archive_title() returns an array get_the_archive_title() returns a string
-
the_archive_title()回显标题,而get_the_archive_title()不回显,因此它们基本相同.如果需要更改内容,则可以使用get.the_archive_title() echoes the title and get_the_archive_title() does not, so they are basically the same. If you need to alter the content then you would use get.
- 1
- 2018-08-19
- Robert Went
-
- 2018-01-27
Ben Gillbanks 有一个不错的解决方案处理所有帖子类型和分类法:
function hap_hide_the_archive_title( $title ) { // Skip if the site isn't LTR, this is visual, not functional. // Should try to work out an elegant solution that works for both directions. if ( is_rtl() ) { return $title; } // Split the title into parts so we can wrap them with spans. $title_parts = explode( ': ', $title, 2 ); // Glue it back together again. if ( ! empty( $title_parts[1] ) ) { $title = wp_kses( $title_parts[1], array( 'span' => array( 'class' => array(), ), ) ); $title = '<span class="screen-reader-text">' . esc_html( $title_parts[0] ) . ': </span>' . $title; } return $title; } add_filter( 'get_the_archive_title', 'hap_hide_the_archive_title' );
Ben Gillbanks has a nice solution that handles all post types and taxonomies:
function hap_hide_the_archive_title( $title ) { // Skip if the site isn't LTR, this is visual, not functional. // Should try to work out an elegant solution that works for both directions. if ( is_rtl() ) { return $title; } // Split the title into parts so we can wrap them with spans. $title_parts = explode( ': ', $title, 2 ); // Glue it back together again. if ( ! empty( $title_parts[1] ) ) { $title = wp_kses( $title_parts[1], array( 'span' => array( 'class' => array(), ), ) ); $title = '<span class="screen-reader-text">' . esc_html( $title_parts[0] ) . ': </span>' . $title; } return $title; } add_filter( 'get_the_archive_title', 'hap_hide_the_archive_title' );
-
- 2018-04-17
您可以使用
post_type_archive_title()
获取没有" Archives:"文本的存档标题.You can use
post_type_archive_title()
to get the title of an archive without the "Archives:" text.
在我的子主题的
archive.php
中,我具有以下用于显示存档页面标题的代码:但是这会将我的标题显示为"类别:类别标题",而不是简单地显示没有前置"类别:"的标题.
我的第一个直觉是从
wp-includes/general-template
中覆盖wp-includes/general-template
.但是从我所读的内容来看,即使使用儿童主题的替代内容,显然也不应该更改wordpress的核心内容.那么控制
the_archive_title()
输出的最佳实践方法是什么?