【问题标题】:Limit the number of taxonomy terms diplayed限制显示的分类术语的数量
【发布时间】:2017-01-11 20:41:06
【问题描述】:

我有 2 个分类集 product_cattvshows_cat。每组有 12 个术语。

一个产品最多可以有 12 个术语(但不能同时来自 2 个集合)

我使用此代码在产品页面中显示术语列表:

$cats = get_the_term_list($post->ID, 'product_cat', '', '  ', ''); 
$tvcats = get_the_term_list($post->ID, 'tvshows_cat', '', '  ', ''); 

if (!empty($cats)){
    echo strip_tags($cats, '   ');
}elseif(!empty($tvcats)){
    echo strip_tags($tvcats, '    ');
}

结果是:

动作、剧情、冒险、传记、动画

问题在于,在某些情况下,没有足够的空间来显示所有术语。

我需要将术语数量限制为 2 个术语。

问题:

我怎样才能限制适用于两个术语的数量?

谢谢

【问题讨论】:

    标签: php wordpress woocommerce categories custom-taxonomy


    【解决方案1】:

    我假设您的最终输出是逗号分隔的字符串 - 动作、戏剧、冒险、传记、动画。

    你可以只显示两个项目

    $items = "item1, item2, item3, item4";
    
    $filter = explode(',', $items);
    
    for( $i=0; $i<2; $i++ ) {
        echo $filter[$i];
    }
    

    尝试用以下代码替换您上面提供的代码

        function display_limited_terms( $items ) {
            $filter = explode(',', $items);
    
            for( $i=0; $i<2; $i++ ) {
                echo $filter[$i];
            }
        }
    
    
        $cats = get_the_term_list($post->ID, 'product_cat', '', '  ', '');
        $tvcats = get_the_term_list($post->ID, 'tvshows_cat', '', '  ', '');
    
        if (!empty($cats)){
    
            display_limited_terms( strip_tags($cats, '   ') );
        }
    
        elseif(!empty($tvcats)) {
    
            display_limited_terms( strip_tags($cats, '   ') );
        }
    

    【讨论】:

    • 我该如何使用它?放在哪里?
    【解决方案2】:

    你也可以使用explode()array_slice() 来解决这个问题。

    例如:

    function display_limited_terms($items){
        $filter = explode(',', $items);
        $a = array_slice($filter, 0, 2);
        foreach ($a as $b) {
            echo $b;
        }
    }
    
    
    $cats = get_the_term_list($post->ID, 'product_cat', '', '  ', '');
    $tvcats = get_the_term_list($post->ID, 'tvshows_cat', '', '  ', '');
    
    if (!empty($cats)) {
    
        display_limited_terms(strip_tags($cats, '   '));
    } elseif (!empty($tvcats)) {
    
        display_limited_terms(strip_tags($cats, '   '));
    }
    

    【讨论】:

    • komak khasti bego .
    • 我不想使用function.php。
    • 你没有使用 function.php 来解决这个问题。只需在模板文件中添加顶部代码。
    【解决方案3】:

    您应该使用get_the_term_list() 函数,而不是使用get_the_terms(),这将直接为您提供一系列术语对象 (如果您看,get_the_term_list() 正在使用get_the_terms() 她自己到函数的源代码)

    然后你可以构建一个自定义函数来获得你想要的(我将不使用 implode() 函数任何其他函数 php 函数,因为我们只需要 2 个术语。)

    注意:这里不需要strip_tags()函数

    所以你的代码将是:

    // This function goes first
    function get_my_terms( $post_id, $taxonomy ){
        $cats = get_the_terms( $post_id, $taxonomy );
    
        foreach($cats as $cat) $cats_arr[] = $cat->name;
    
        if ( count($cats_arr) > 1) $cats_str = $cats_arr[0] . ', ' . $cats_arr[1]; // return first 2 terms
        elseif ( count($cats_arr) == 1) $cats_str = $cats_arr[0]; // return one term
        else $cats_str = '';
    
        return $cats_str;
    }
    

    此代码位于您的活动子主题(或主题)或任何插件文件的 function.php 文件中……

    那么下面是你的代码:

    $cats = get_my_terms( $post->ID, 'product_cat' ); 
    $tvcats = get_my_terms( $post->ID, 'tvshows_cat' ); 
    
    // Displaying 2 categories terms max
    echo $cats . $tvcats;
    

    此代码位于您的 php 模板文件中


    ——更新—— (与作者评论有关)

    或者没有功能,你的代码将是:

    // Product categories
    $cats = get_the_terms( $post->ID, 'product_cat' );
    
    foreach($cats as $cat) $cats_arr[] = $cat->name;
    
    if ( count($cats_arr) > 1) $cats_str = $cats_arr[0] . ', ' . $cats_arr[1]; // return first 2 terms
    elseif ( count($cats_arr) == 1) $cats_str = $cats_arr[0]; // return one term
    else $cats_str = '';
    
    // TV shows categories
    $tvcats = get_the_terms( $post->ID, 'tvshows_cat' );
    
    foreach($tvcats as $tvcat) $tvcat_arr[] = $tvcat->name;
    
    if ( count($tvcat_arr) > 1) $tvcats_str = $tvcat_arr[0] . ', ' . $tvcat_arr[1]; // return first 2 terms
    elseif ( count($tvcat_arr) == 1) $tvcats_str = $tvcat_arr[0]; // return one term
    else $tvcats_str = '';
    
    // The Display of 2 categories
    echo $cats_str . $tvcats_str;
    

    此代码位于您的 php 模板文件中

    此代码已经过测试并且可以工作。

    【讨论】:

    • 我们可以只使用模板代码吗?我们如何只在模板中使用代码,而不是函数?
    • 可以,但最好使用没有自定义功能的替代版本。
    猜你喜欢
    • 1970-01-01
    • 2011-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-05
    • 2012-10-12
    • 2015-02-23
    • 1970-01-01
    相关资源
    最近更新 更多