¿Cómo personalizar the_archive_title ()?
-
-
Publique su solución como una respuesta separada.Tambiénpuede rechazarelmío y aceptarel suyo si lo desea :-)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
-
Vaya,pensé que habíaeliminadoestaedición.Tumétodoterminó siendomejor,así que quiseborrarelmío.Lo haré ahora.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 respuestas
- votos
-
- 2015-01-25
Si observa el códigofuente de
get_the_archive_title()
,verá que hay unfiltroproporcionado,llamadoget_the_archive_title
,através del cualpuedefiltrar la salida de lafunción.Puede utilizar lo siguientepara cambiar la salidaen unapágina de categoría
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; });
-
Estonofuncionaen unapágina de archivo con untítulo de `Archivo: Libros` (donde` Libros`es untipo depublicaciónpersonalizada).Lafunción `single_cat_title ()` solo devuelve untítulo depágina sieltipoes * categoría * o *etiqueta *.¿Puedemodificar su respuestaparaincluir una solución quefuncione contodos lostipos de contenido?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 Mirael códigofuente,líneas 1239 y 1240 :-).Sitiene unproblema,simplemente haga unanuevapreguntaespecíficapara supregunta@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
-
Correcto,uno debe devolver untítulo alternativopara cadataxonomía otipo depublicaciónpara la que deseantítulos diferentes.Publiqué una respuesta queexcluye losprefijos detodos lostipos.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 respuesta aceptadafuncionaparaeliminarelprefijo
Category:
de lostítulos de archivo de categorías,perono otrastaxonomías otipos depublicaciones. Paraexcluir otrosprefijos,hay dos opciones:-
Reconstruyaeltítuloparatodas las variantes utilizadasen lafunción
get_the_archive_title()
original:// 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; });
-
O simplementeeliminetodo lo queparezca unprefijo detítulo (lo quepuede alterar lostítulos reales que contienen unapalabra seguidapor dospuntos):
// 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); });
-
Paratítulos que contienenespacios como `tipo deenlace:tv`,laexpresión regular debe ser`/^ (\ w| \ s) +:/`For titles containing spaces like `link type: tv` the regex must be `/^(\w|\s)+: /`
- 0
- 2020-04-19
- aitor
-
-
Estonofuncionaen unapágina de archivo con untítulo de Archivo: Libros (donde Libroses untipo depublicaciónpersonalizada).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/) usa` single_term_title`internamente,quetambiénfuncionaen taxonomías.Asegúrate de quetutemanotenga una de las siguientesplantillas definidas: `taxonomy-book.php` o`taxonomy.php`,porquetienen precedencia sobre `archive.php`.Además,considereprobar cualquiera de losmétodosen las otras respuestas.Esta respuestaesbastante antigua,funcionababien enelmomentoen que lapubliqué,pero yanoestoy desarrollandopara 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
Otra opciónes:
<?php echo str_replace('Brand: ','',get_the_archive_title()); ?>
Reemplazarmarca: con cualquiertexto del que desee deshacerse.
Vale lapena analizar la diferenciaentreget_the_archive_title () ythe_archive_title () the_archive_title () devuelve unamatriz get_the_archive_title () devuelve una cadena
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 () se haceeco deltítulo yget_the_archive_title ()no,por lo que sonbásicamente lomismo.Sinecesitamodificarel contenido,utiliceget.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 tiene unabuena solución quemanejatodos lostipos depublicaciones ytaxonomías:
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
Puede utilizar
post_type_archive_title()
para obtenereltítulo de un archivo sineltexto "Archives:".You can use
post_type_archive_title()
to get the title of an archive without the "Archives:" text.
Enel
archive.php
demi tema hijo,tengoel siguiente códigoparamostrareltítulo demispáginas de archivo:Peroesomuestramistítulos como "Categoría: Título de categoría "en lugar de simplementeeltítulo sin la "Categoría:" antepuesta.
Miprimerinstintofue anular
get_the_archive_title()
dewp-includes/general-template
.Peropor lo que he leído,aparentemente se supone queno debo alterarnuncaelmaterialprincipal de WordPress,incluso con anulaciones de untema hijo.Entonces,¿cuáles lamejorforma de controlar la salida de
the_archive_title()
?