I have the following code to show the names of the categories for a single post. My categories are named in the plural (i.e. articles, letters, translations, commentaries, etc.), but in this function I want to return a singular version of the name, i.e. article, letter, translation, commentary, etc.).
$categories = get_the_terms( $post->ID, 'category' );
if( !empty( $categories ) && !is_wp_error( $categories ) ) {
foreach( $categories as $category ) {
$catdisplay = sprintf(
'<a target="_blank" href="%1$s">%2$s</a>%3$s',
esc_url( get_term_link( $category ) ),
esc_html( $category->name ),
'<span class="cat-sep">, </span>'
);
echo sprintf( esc_html__( '%s', 'textdomain' ), $catdisplay );
}
}
I was thinking to build in something with the following logic: if name = "articles" return "article", or if name returned = "commentaries" return "commentary", but am unsure how to accomplish that or if there's a better way to do it. I'm also open to better ways to structure the above code.
Edit: would it be possible to use something like strpos()
or str_replace()
or similar functions? i.e. if %2$s = 'Articles' replace with 'Article'?
I have the following code to show the names of the categories for a single post. My categories are named in the plural (i.e. articles, letters, translations, commentaries, etc.), but in this function I want to return a singular version of the name, i.e. article, letter, translation, commentary, etc.).
$categories = get_the_terms( $post->ID, 'category' );
if( !empty( $categories ) && !is_wp_error( $categories ) ) {
foreach( $categories as $category ) {
$catdisplay = sprintf(
'<a target="_blank" href="%1$s">%2$s</a>%3$s',
esc_url( get_term_link( $category ) ),
esc_html( $category->name ),
'<span class="cat-sep">, </span>'
);
echo sprintf( esc_html__( '%s', 'textdomain' ), $catdisplay );
}
}
I was thinking to build in something with the following logic: if name = "articles" return "article", or if name returned = "commentaries" return "commentary", but am unsure how to accomplish that or if there's a better way to do it. I'm also open to better ways to structure the above code.
Edit: would it be possible to use something like strpos()
or str_replace()
or similar functions? i.e. if %2$s = 'Articles' replace with 'Article'?
1 Answer
Reset to default 0Not an exact answer for original post's code setup, but since I came here to answer the same question according to the title I'll provide my not so elegant solution using ACF (though the same can surely be done with a regular custom field):
TO DISPLAY A SINGULAR VERSION OF YOUR CATEGORY NAME (Example using product cat for woo):
Setup a custom field for the category archive
Name it singular
Implement the custom field in your template by using the following (untested, but code generated using ACF Theme Code plugin):
// Define taxonomy prefix eg category // Use term for all taxonomies $taxonomy_prefix = 'product_cat'; // Define term ID // Replace NULL with ID of term to be queried eg '123' $term_id = NULL; // Example: Get the term ID in a term archive template // $term_id = get_queried_object_id(); // Define prefixed term ID $term_id_prefixed = $taxonomy_prefix .'_'. $term_id; ?> <?php the_field( 'singular_name', $term_id_prefixed ); ?>```
esc_html( $category->description )
. – Shazzad Commented Apr 19, 2020 at 0:10