###■目的
WordPressでタームの日にちをソートしたいです。
※ただし、タームは「01/01」でなく「1/1」で入力しています。
###■現状
まず出力されるHTMLはこちらです。
html
1<div class="day-area"> 2<li class="day"><a href="xxx">1/1</a></li> 3<li class="day"><a href="xxx">10/1</a></li> 4<li class="day"><a href="xxx">2/1</a></li> 5</div>
1/1、2/1、10/1
と日付順に表示したいのに、
1/1、10/1、2/1、
と数字順で表示されてしまうのです。
###■試したこと
まず下記のjavascriptを書いてみたのですけれど、ぜんぜんソートしてくれません。(>_<)
javascript
1(function($){ 2 $(function(){ 3 $(document).ready( function(){ 4 var arr = document.querySelector(".day"); 5 6 arr.sort(function(a,b) { 7 return (a.date < b.date ? 1 : -1); 8 }); 9 10 }); 11 }); 12})(jQuery); 13
次に下記のphpです。これはソートできたのですが、これでは日付ターム一覧のアーカイブページの表示において不具合(1つの記事しか表示されず、javascriptも読み込まない、など)が起こってしまうのでダメです。
php
1<div class="day-area"> 2 <?php 3 // タームを表示する関数 4 $terms = get_the_terms($post->ID,'tag_day'); 5 usort($terms, 'tag_day_sort'); 6 foreach ( $terms as $term ) { 7 echo '<li class="day"><a href="'.get_term_link($term).'">'.$term->name.'</a></li>'; 8 } 9 // 並べ替えの関数 10 function tag_day_sort($arg1, $arg2) { 11 $time1 = strtotime($arg1->name); 12 $time2 = strtotime($arg2->name); 13 if ($time1 == $time2) { return 0; } 14 return ($time1 < $time2) ? -1 : 1; 15 } 16 ?> 17</div>
以上2つ以外の方法を募集しております。
よろしくお願い致します。m(_ _)m
回答1件
あなたの回答
tips
プレビュー