Php 仅显示产品的第一个和最后一个分类术语
我有一个自定义的分类法(年),每年都是一个术语。有时一部电影的拍摄时间超过一年。我只需要打印一部电影中的Php 仅显示产品的第一个和最后一个分类术语,php,wordpress,woocommerce,product,custom-taxonomy,Php,Wordpress,Woocommerce,Product,Custom Taxonomy,我有一个自定义的分类法(年),每年都是一个术语。有时一部电影的拍摄时间超过一年。我只需要打印一部电影中的(第一个-最后一个)全年否 例如,我这几年一直在写吸血鬼日记: 2008年、2009年、2010年、2011年、2012年、2013年、2014年、2015年和2016年 我只想以这种方式展示第一年和最后一年:吸血鬼日记(2008-2016) 我的代码是: <?php $releaseyear_as_text = get_the_term_list( $post->ID,'rele
(第一个-最后一个)
全年否
例如,我这几年一直在写吸血鬼日记:2008年、2009年、2010年、2011年、2012年、2013年、2014年、2015年和2016年 我只想以这种方式展示第一年和最后一年:吸血鬼日记(2008-2016) 我的代码是:
<?php $releaseyear_as_text = get_the_term_list( $post->ID,'release-year','', ' , ' ); ?>
<h1><a href="<?php the_permalink() ?>"><?php the_title(); ?> (<?php echo strip_tags($releaseyear_as_text) ?>)</a></h1>
<div class="clearfix"></div>
我怎样才能做到这一点
谢谢我已经在下面编写了一些代码,这些代码将准确地显示您所期望的内容。此外,它还将处理另外两种情况:
- 只有一个任期(一年)时
- 无期限(无年份)时
<?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>