Last active
February 14, 2025 15:02
-
-
Save sybrew/4e0fc4849abcfdbc6df778cb9af9fff5 to your computer and use it in GitHub Desktop.
Generates a custom title and description for taxonomy terms.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Do not include the PHP opening tag if PHP is already open | |
// Per https://wordpress.org/support/topic/term_title-in-custom-taxonomies-archive-titles/ | |
add_filter( | |
'the_seo_framework_generated_archive_title_items', | |
/** | |
* Filters the generated archive title items. | |
* | |
* @param String[title,prefix,title_without_prefix] $items The generated archive title items. | |
* @param \WP_Term|\WP_User|\WP_Post_Type|null $object The archive object. | |
* Is null when query is autodetermined. | |
* @return array The modified archive title items. | |
*/ | |
function ( $items, $object ) { | |
$object ??= get_queried_object(); | |
if ( empty( $object ) ) | |
return $items; | |
[ &$title, $prefix, &$title_without_prefix ] = $items; | |
switch ( $object->taxonomy ?? '' ) { | |
case 'topics': | |
$title = "Read the latest news and updates about {$object->name}"; | |
break; | |
} | |
$title_without_prefix = $title; | |
if ( tsf()->title()->conditions()->use_generated_archive_prefix( $object ) ) { | |
$title = \sprintf( | |
/* translators: 1: Title prefix. 2: Title. */ | |
\_x( '%1$s %2$s', 'archive title', 'default' ), | |
$prefix, | |
$title, | |
); | |
} | |
return $items; | |
}, | |
10, | |
2, | |
); | |
add_filter( | |
'the_seo_framework_generated_description', | |
/** | |
* Filters the generated description. | |
* | |
* @param string $desc The generated description. | |
* @param array|null $args The query arguments. Contains 'id', 'tax', 'pta', and 'uid'. | |
* Is null when the query is auto-determined. | |
* @return string The modified description. | |
*/ | |
function ( $desc, $args ) { | |
if ( null === $args ) { | |
// Front end. | |
$tsf_query = tsf()->query(); | |
$taxonomy = $tsf_query->get_current_taxonomy(); | |
$id = $tsf_query->get_the_real_id(); | |
} elseif ( 'term' === The_SEO_Framework\get_query_type_from_args( $args ) ) { | |
$taxonomy = $args['tax']; | |
$id = $args['id']; | |
} | |
if ( empty( $taxonomy ) || empty( $id ) ) | |
return $desc; | |
$term = get_term( $id, $taxonomy ); | |
if ( empty( $term->name ) ) | |
return $desc; | |
switch ( $taxonomy ) { | |
case 'institutie': | |
$desc = "Descoperiți articole despre {$term->name}, incluzând știri și analize despre activitatea acestei instituții."; | |
break; | |
case 'companii': | |
$desc = "Descoperiți informații despre {$term->name}, incluzând știri și analize despre activitatea acestei companii."; | |
break; | |
case 'persoane_mentionate': | |
$desc = "Citiți articole despre {$term->name}, incluzând detalii și știri despre această persoană."; | |
break; | |
case 'tari_mentionate': | |
$desc = "Aflați mai multe despre {$term->name}, incluzând știri și evenimente relevante despre această țară."; | |
break; | |
case 'etichete': | |
$desc = "Explorați articole marcate cu {$term->name}, incluzând știri și informații relevante."; | |
break; | |
} | |
return $desc; | |
}, | |
10, | |
2, | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment