Comment personnaliser the_archive_title ()?
-
-
Publiez votre solution sousforme de réponse distincte.Vouspouvez également refuser lemien et accepter le vôtre si vous le souhaitez :-)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
-
Oups,je pensais avoir supprimé cettemodification.Votreméthode afini par êtremeilleure,alorsje voulais supprimer lamienne.Je vaisfaire çamaintenant.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 réponses
- votes
-
- 2015-01-25
Si vous regardez le code source de
get_the_archive_title()
,vousverra qu'unfiltreestfourni,appeléget_the_archive_title
,àtravers lequel vouspouvezfiltrer la sortie de lafonction.Vouspouvez utiliser ce qui suitpourmodifier la sortie sur unepage de catégorie
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; });
-
Celane fonctionnepas sur unepage d'archive avec letitre `Archive: Books` (où` Books`est untype depublicationpersonnalisé).Lafonction `single_cat_title ()`ne renvoie untitre depage que si letypeest une * catégorie * ou une *balise *.Pouvez-vousmodifier votre réponsepourinclure une solution quifonctionne avectous lestypes de contenu?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 Regardez le code source,lignes 1239et 1240 :-).Si vous avez unproblème,posez simplement unenouvelle question spécifique à votre question@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
-
À droite,ilfaut renvoyer un autretitrepour chaquetaxonomie outype depublicationpour lequelils veulent destitres différents.J'aipublié une réponse quiexclut lespréfixes detous lestypes.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
La réponse acceptéefonctionnepour supprimer lepréfixe
Category:
destitres des archives de catégorie,maispas d'autrestypes detaxonomie ou depublication. Pourexclure d'autrespréfixes,ilexiste deux options:-
Reconstruisez letitre detoutes les variantes utilisées dans lafonction d'origine
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; });
-
Ou supprimez simplementtout ce qui ressemble à unpréfixe detitre (ce quipeutmodifier lestitres réels qui contiennent unmot suivi du caractère deux-points):
// 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); });
-
Pour lestitres contenant desespacestels que `linktype:tv`,l'expression régulière doit être`/^ (\ w| \ s) +:/`For titles containing spaces like `link type: tv` the regex must be `/^(\w|\s)+: /`
- 0
- 2020-04-19
- aitor
-
-
Celane fonctionnepas sur unepage d'archive avec letitre Archive: Livres (où Livresest untype depublicationpersonnalisé).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/) utilise` single_term_title`en interne,quifonctionne également sur lestaxonomies.Assurez-vous que votrethèmen'apas l'un desmodèles suivants définis: `taxonomy-book.php` ou`taxonomy.php`,carils ont lapriorité sur `archive.php`.Envisagez également detester l'une desméthodes des autres réponses.Cette réponseest assez ancienne,ellefonctionnaitbien aumoment oùje l'aipubliée,maisje ne développepluspour 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
Une autre optionest:
<?php echo str_replace('Brand: ','',get_the_archive_title()); ?>
Remplacez Marque:par letexte dont vous souhaitez vous débarrasser.
Cela vaut lapeine d'examiner la différenceentreget_the_archive_title ()et the_archive_title () the_archive_title () renvoie untableau get_the_archive_title () renvoie une chaîne
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 ()fait écho autitreet get_the_archive_title ()ne lefaitpas,doncils sontfondamentalement lesmêmes.Si vous avezbesoin demodifier le contenu,vous utiliserezget.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 a unebonne solution quigèretous lestypes demessageset toutes lestaxonomies:
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
Vouspouvez utiliser
post_type_archive_title()
pour obtenir letitre d'une archive sans letexte "Archives:".You can use
post_type_archive_title()
to get the title of an archive without the "Archives:" text.
Dans
archive.php
demonthèmeenfant,j'ai le code suivantpour afficher letitre demespages d'archives:Mais cela affichemestitres comme "Catégorie: Titre de la catégorie " au lieu de simplement letitre sans lepréfixe "Catégorie:".
Monpremierinstinct a été de remplacer
get_the_archive_title()
dewp-includes/general-template
.Mais d'après ce quej'ai lu,apparemment,je ne suispas censémodifier les éléments debase de wordpress,même avec les remplacements d'unthèmeenfant.Alors,quelleest lameilleurefaçon de contrôler la sortie de
the_archive_title()
?