我使用以下代码在前端实现过滤器;
//placed this code in index.php after <?php get_header(); ?>
<?php
$search = isset( $_GET[\'s\'] ) ? $_GET[\'s\']: null;
if( $search ){
$user_filtre .= \'s=\' . $_GET[\'s\'] . \'&\';
}
$filtre = isset( $_GET[\'filtre\'] ) ? $_GET[\'filtre\']: \'date\';
$cat = isset( $_GET[\'cat\'] ) ? $_GET[\'cat\']: 0;
$user_filtre = \'filtre=\' . $filtre . \'&\';
$display = isset( $_GET[\'display\'] ) ? $_GET[\'display\']: get_option( get_theme_name() . \'_type_display\' );
$user_display = \'display=\' . $display . \'&\';
if( ($filtre != null) && ($cat >= 0) ) :
$args = array(
\'posts_per_page\' => get_option( get_theme_name() . \'_nb_videos_listing\' ),
\'post_type\' => \'post\',
\'paged\' => $paged,
);
$tri = null;
switch( $filtre ){
case \'date\' :
$args = array(
\'orderby\' => \'date\',
\'order\' => \'DESC\',
\'posts_per_page\' => get_option( get_theme_name() . \'_nb_videos_listing\' ),
\'paged\' => $paged,
\'cat\' => $cat
);
break;
case \'views\' :
$args = array(
\'meta_key\' => \'post_views_count\',
\'orderby\' => \'meta_value_num\',
\'order\' => \'DESC\',
\'posts_per_page\' => get_option( get_theme_name() . \'_nb_videos_listing\' ),
\'paged\' => $paged,
\'cat\' => $cat
);
break;
//for longest videos
case \'duree\' :
$args = array(
\'meta_key\' => \'complete\', // custom feild storing time in decimal
\'orderby\' => \'meta_value_num\',
\'order\' => \'DESC\',
\'posts_per_page\' => get_option( get_theme_name() . \'_nb_videos_listing\' ),
\'paged\' => $paged,
\'cat\' => $cat
);
break;
//for longest videos
case \'short\' :
$args = array(
\'meta_key\' => \'complete\', // custom feild storing time in decimal
\'orderby\' => \'meta_value_num\',
\'order\' => \'ASC\',
\'posts_per_page\' => get_option( get_theme_name() . \'_nb_videos_listing\' ),
\'paged\' => $paged,
\'cat\' => $cat
);
break;
case \'random\' :
$args = array(
\'orderby\' => \'rand\',
\'posts_per_page\' => get_option( get_theme_name() . \'_nb_videos_listing\' ),
\'paged\' => $paged,
\'cat\' => $cat
);
break;
default:
$args = array(
\'posts_per_page\' => get_option( get_theme_name() . \'_nb_videos_listing\' ),
\'post_type\' => \'post\',
\'paged\' => $paged
);
break;
}
$tri = /*\'\' . __(\'sorted by\', get_theme_name()) . \'\' . */corres_tv( $filtre );
$term = $wp_query->queried_object;
$wp_query = new WP_Query( $args );
?>
并使用以下行在前端显示筛选器列表
<ul class="filtre-list">
<!-- for latest videos -------> <li <?php echo ($filtre == \'date\') ? \'class="selected"\' : \'\'; ?>><a href="?<?php echo $user_display;?>filtre=date"><?php echo __(\'lastest\', get_theme_name());?></a></li>
<!-- for Most viewed videos --> <li <?php echo ($filtre == \'views\') ? \'class="selected"\' : \'\'; ?>><a href="?<?php echo $user_display;?>filtre=views"><?php echo __(\'most viewed\', get_theme_name());?></a></li>
<!-- for longest videos ------> <li <?php echo ($filtre == \'duree\') ? \'class="selected"\' : \'\'; ?>><a href="?<?php echo $user_display;?>filtre=duree"><?php echo __(\'longest\', get_theme_name());?></a></li>
<!-- for shortest videos -----> <li <?php echo ($filtre == \'short\') ? \'class="selected"\' : \'\'; ?>><a href="?<?php echo $user_display;?>filtre=short"><?php echo __(\'shortest\', get_theme_name());?></a></li>
<!-- for Random videos -------> <li <?php echo ($filtre == \'random\') ? \'class="selected"\' : \'\'; ?>><a href="?<?php echo $user_display;?>filtre=random"><?php echo __(\'random\', get_theme_name());?></a></li>
</ul><!-- .filtre-list -->
瞧!它起作用了!