最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

categories - How to display singular form of category name?

programmeradmin3浏览0评论

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'?

Share Improve this question edited Apr 19, 2020 at 4:14 Jon Fergus asked Apr 18, 2020 at 20:37 Jon FergusJon Fergus 791 silver badge10 bronze badges 2
  • You could use the category description field (if not already using for other purpose). Write your desired text in the description, and in the code use esc_html( $category->description ). – Shazzad Commented Apr 19, 2020 at 0:10
  • I'd like to use the description on archive pages, so unfortunately that won't work for me. – Jon Fergus Commented Apr 19, 2020 at 1:38
Add a comment  | 

1 Answer 1

Reset to default 0

Not 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):

  1. Setup a custom field for the category archive

  2. Name it singular

  3. 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 ); ?>```
    
    
    
发布评论

评论列表(0)

  1. 暂无评论