简体   繁体   English

如果在wordpress中show_count小于2,如何使输出显示Type而不是Types

[英]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. 所以我正在使用wp_list_categories输出一组列表,而show_count等于true这样我就可以看到该类别中的帖子数量。

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. 无论如何,我设法在我的functions.php中使用了这段代码,并删除了我的帖子计数中的括号,将其用一个跨距包裹起来,并在数字后加上"Types"

The problem is that if I have only 1 post assigned to that category it shouldn't be plural and say "Types" . 问题是,如果我只给该类别分配了1个帖子,则不应为复数形式并说"Types" If it equals less than 2 it should say "Type" . 如果它小于2,则应说"Type"

Here is my code that I use in my functions.php for that: 这是我在functions.php中使用的代码:

/**
 * 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: 我只需要在其中添加一个条件即可定义show_count ,并说:

if it equals less than 2 echo "Type" else echo "Types" . 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 不确定$ links的结构,但是可以使用count()如果它是一个索引数组来获取链接数,例如

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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