仅显示产品分类的第一个和最后一个术语 [英] Display only the first and last term of taxonomy for a product

查看:86
本文介绍了仅显示产品分类的第一个和最后一个术语的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义分类法(年份),每年都是一个学期。有时电影已经超过一年了。我只需要打印 (第一张-最后一张) 年,而不必从一年中的所有电影开始打印。

I have a custom taxonomy (Year) and each year is a term. Some times a film has more than one year. I need to print only the (first - Last) year no all year from one film.

例如我今年有吸血鬼日记:
2008、2009、2010、2011、2012、2013、2014、2015和2016

For example I have this years for Vampire diaries:
2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 and 2016

我只想以此方式显示头几年和最后几年:吸血鬼日记(2008-2016)

I would like to only display the first and Last years this way: Vampire diaries (2008 - 2016)

我的代码是:

<?php $releaseyear_as_text = get_the_term_list( $post->ID,'release-year','', '  ,  ' ); ?>

    <h1><a href="<?php the_permalink() ?>"><?php the_title(); ?>&nbsp;(<?php echo strip_tags($releaseyear_as_text) ?>)</a></h1>
    <div class="clearfix"></div>

如何实现?

谢谢

推荐答案

我在下面编写了一些代码,这些代码将准确显示您罕见的期望。此外,它将处理另外2种情况:

I have write some code below which will display exactly what you rare expecting. Additionally it will handle that 2 other cases:


  • 只有一个学期(一年)时

  • 没有期限(无年份)

这是您的自定义代码:

<?php

// Getting the years for the current post ID in a string coma separated
$release_years_str = get_the_term_list( $post->ID,'release-year','', ',' );

// concerting years string in an array of years
$release_years_arr = explode(',', $release_years_str);

// Number of items (terms) in the array
$count = sizeof( $release_years_arr );

// First term year
$first_year = $release_years_arr[ 0 ];

// if there is more than on term (year)
if ( $count > 1 ) 
{
    // Last term year
    $last_year = $release_years_arr[ $count - 1 ];

    // Formatting in a string the 2 years: (first - Last)
    $releaseyear_as_text = ' (' . $first_year . ' - ' . $last_year . ')';

}
elseif ($count == 1) // If there is only one term (one year in the array)
{
    $releaseyear_as_text = ' (' . $first_year . ')';
}
else // No term (No years, empty array)
{
    $releaseyear_as_text = '';
}
?>

<h1><a href="<?php the_permalink() ?>"><?php the_title(); echo strip_tags($releaseyear_as_text); ?></a></h1>
<div class="clearfix"></div>






WordPress函数参考wp获取发布条款

这篇关于仅显示产品分类的第一个和最后一个术语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆