2016-08-20 3 views
3

사용자 분류 (Year)가 있으며 각 연도는 용어입니다. 영화의 경우 1 년 이상 된 경우가 있습니다. 하나의 필름으로 만 (first - Last) 년을 인쇄 할 필요가 있습니다.
2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 및 2016제품 분류 체계의 첫 번째 및 마지막 용어 만 표시

난 단지 이런 식으로 첫 번째와 마지막 년을 표시 할 것 :

예를 들어 나는 뱀파이어 다이어리이 년이 : 뱀파이어 다이어리 (2,008에서 2,016 사이)

내 코드는 다음과 같습니다

<?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> 

내가 어떻게 이것을 달성 할 수 있습니까?

감사합니다.

답변

1

희소 한 것을 정확하게 표시하는 코드를 작성했습니다. 또한 그것은 그이 다른 경우를 처리합니다 :

  • 만 한 학기 (1 년)
  • 더 용어가 없다 (NO 년) 여기

이다 사용자 정의 코드가있는 경우 :

<?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 Function Reference wp get post terms

관련 문제