Last active
July 6, 2018 19:25
-
-
Save kaoru-fukusato/2f628b69f788d1ce4bc07d28cd83bbef to your computer and use it in GitHub Desktop.
カテゴリとタグのアーカイブページに4種類の並び替えソート
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//一般的にはindex.phpか、header.phpあたり | |
<?php | |
if ( is_archive() || is_search() ) { //アーカイブか検索ページだったら | |
global $wp_query; | |
$total_results = $wp_query->found_posts; //件数を取得しておく | |
} | |
?> | |
<?php if ( is_archive() ): //アーカイブページ ?> | |
<?php if ( is_category() || is_tag() ): //カテゴリ,タグアーカイブ ?> | |
<div class="result"> | |
<?php if( is_category() ): //カテゴリアーカイブのとき ?> | |
カテゴリ "<?php single_cat_title(); ?>":<?php echo $total_results; ?>件<br /> | |
<?php elseif( is_tag() ): //タグアーカイブのとき ?> | |
タグ "<?php single_tag_title(); ?>":<?php echo $total_results; ?>件<br /> | |
<?php endif; ?> | |
//並び替えボタンの実装 | |
</div> | |
<?php elseif( is_date() ): //日付アーカイブ ?> | |
<div class="result"> | |
<?php if( $year ): echo $year ?>年<?php endif; ?> | |
<?php if( $monthnum ): echo $monthnum ?>月<?php endif; ?> | |
<?php if( $day ): echo $day ?>日<?php endif; ?> | |
:<?php echo $total_results; ?>件 | |
</div> | |
<?php endif; ?> | |
<?php endif; ?> | |
<?php if ( is_search() ): //検索 ?> | |
<div class="result">"<?php the_search_query(); ?>" で検索した結果:<?php echo $total_results; ?>件</div> | |
<?php endif; ?> | |
//ボタンを4つ並べる | |
<?php | |
$url_str = get_pagenum_link(1); | |
$sortset = (string)filter_input(INPUT_GET, 'sort'); //取得 | |
?> | |
並び替え: | |
<form method="get" action="<?php echo $url_str ?>"> | |
<input type="hidden" name="sort" value="newer" /> | |
<?php if ( $sortset !== 'older' && $sortset !== 'popular' && $sortset !== 'unpopular' ): ?> | |
<input type="submit" class="sort_crt" value="新しい" /> | |
<?php else: ?> | |
<input type="submit" value="新しい" /> | |
<?php endif; ?> | |
</form> | |
| | |
<form method="get" action="<?php echo $url_str ?>"> | |
<input type="hidden" name="sort" value="older" /> | |
<?php if ( $sortset === 'older' ): ?> | |
<input type="submit" class="sort_crt" value="古い" /> | |
<?php else: ?> | |
<input type="submit" value="古い" /> | |
<?php endif; ?> | |
</form> | |
| | |
<form method="get" action="<?php echo $url_str ?>"> | |
<input type="hidden" name="sort" value="popular" /> | |
<?php if ( $sortset === 'popular' ): ?> | |
<input type="submit" class="sort_crt" value="閲覧多" /> | |
<?php else: ?> | |
<input type="submit" value="閲覧多" /> | |
<?php endif; ?> | |
</form> | |
| | |
<form method="get" action="<?php echo $url_str ?>"> | |
<input type="hidden" name="sort" value="unpopular" /> | |
<?php if ( $sortset === 'unpopular' ): ?> | |
<input type="submit" class="sort_crt" value="閲覧少" /> | |
<?php else: ?> | |
<input type="submit" value="閲覧少" /> | |
<?php endif; ?> | |
</form> | |
//省コード | |
<?php | |
$url_str = get_pagenum_link(1); //URL | |
$sortset = (string)filter_input(INPUT_GET, 'sort'); //取得 | |
$crt = ' class="sort_current"'; | |
?> | |
並び替え: | |
<form method="get" action="<?php echo $url_str ?>"> | |
<input type="hidden" name="sort" value="newer" /> | |
<input type="submit"<?php if( $sortset !== 'older' && $sortset !== 'popular' && $sortset !== 'unpopular' ){ echo $crt; } ?> value="新しい" /> | |
</form> | |
| | |
<form method="get" action="<?php echo $url_str ?>"> | |
<input type="hidden" name="sort" value="older" /> | |
<input type="submit"<?php if( $sortset === 'older' ){ echo $crt; } ?> value="古い" /> | |
</form> | |
| | |
<form method="get" action="<?php echo $url_str ?>"> | |
<input type="hidden" name="sort" value="popular" /> | |
<input type="submit"<?php if( $sortset === 'popular' ){ echo $crt; } ?> value="閲覧多" /> | |
</form> | |
| | |
<form method="get" action="<?php echo $url_str ?>"> | |
<input type="hidden" name="sort" value="unpopular" /> | |
<input type="submit"<?php if( $sortset === 'unpopular' ){ echo $crt; } ?> value="閲覧少" /> | |
</form> | |
//functions.php | |
function SortArchive( $query ) { | |
if ( is_admin() || ! $query->is_main_query() ) | |
return; | |
if ( $query->is_tag() || $query->is_category() ) { | |
$sortset = (string)filter_input(INPUT_GET, 'sort'); | |
if ( $sortset === 'older' ) { //古い | |
$query->set( 'orderby', 'date' ); | |
$query->set( 'order', 'ASC' ); | |
} elseif ( $sortset === 'popular' ) { //閲覧多 | |
$query->set( 'meta_key', 'views' ); | |
$query->set( 'orderby', 'meta_value_num' ); | |
} elseif ( $sortset === 'unpopular' ) { //閲覧少 | |
$query->set( 'meta_key', 'views' ); | |
$query->set( 'orderby', 'meta_value_num' ); | |
$query->set( 'order', 'ASC' ); | |
} else { //それ以外(新しい) | |
$query->set( 'orderby', 'date' ); | |
} | |
return; | |
} | |
} | |
add_action( 'pre_get_posts', 'SortArchive' ); | |
//style.css | |
.result form{display: inline;} | |
.result input[type="submit"]{ | |
color: #aa9a85; | |
padding: 0; | |
margin: 0; | |
box-shadow: none; | |
background: none; | |
border: none; | |
font-size: 0.85em; | |
text-decoration: underline; | |
cursor: pointer; | |
} | |
.sort_current{ | |
font-weight: bold; | |
text-decoration: none; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment