简体   繁体   中英

How can I make my output say Type and not Types if show_count is less than 2 in wordpress

So I'm using wp_list_categories to output a set of lists and show_count is equal to true so I can see the amount of posts in that category.

Anyways, I managed to use this code in my functions.php and remove the parenthesis around my post counts, wrap it with a span and have it say "Types" after the number.

The problem is that if I have only 1 post assigned to that category it shouldn't be plural and say "Types" . If it equals less than 2 it should say "Type" .

Here is my code that I use in my functions.php for that:

/**
 * filter the wp_list_categories and wrap with <span>
 */

add_action('pre_get_posts', 'mr_modify_archive_taxonomy_query');

$links = add_filter('wp_list_categories', 'cat_count_span');

function cat_count_span($links) {
    $links = str_replace('</a> (', '</a> <span class="pull-right">', $links);
    $links = str_replace(')', ' Types</span>', $links);
    return $links;
}

I just need to add a conditional in there somehow that defines the show_count and have it say:

if it equals less than 2 echo "Type" else echo "Types" .

Just having a really hard time giving it this conditional.

not sure of the structure of $links but you could use count() if its an indexed array to get the number of links eg

add_filter('wp_list_categories', 'cat_count_span');
function cat_count_span($links) {

  $needle ="<li"; // e.g. <li> <a etc.
  $count= substr_count($links, $needle);
  $word= 'Types';

  if($count==1) $word= 'Type';      

  $links = str_replace('</a> (', '</a> <span class="pull-right">', $links);
  $links = str_replace(')', $word.'</span>', $links);
  return $links;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM