繁体   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

所以我正在使用wp_list_categories输出一组列表,而show_count等于true这样我就可以看到该类别中的帖子数量。

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

问题是,如果我只给该类别分配了1个帖子,则不应为复数形式并说"Types" 如果它小于2,则应说"Type"

这是我在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;
}

我只需要在其中添加一个条件即可定义show_count ,并说:

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

给这个条件一个非常困难的时期。

不确定$ 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