-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhide-items.php
72 lines (64 loc) · 1.98 KB
/
hide-items.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
F.1.1 - Just Hide Items ~ Filter printed items (display)
This is some JS and CSS to filter already displayed items.
The idea is that we print all items and use JS to hide when filtering. No ajax is used. Good if you don't have a lot of items
The items need to have a class that shows their category (in the example it is category-[TERMID]). This is usually aready there in WP.
Printed items is not part of the example. Just print them as you like but remember to put the class as mentioned. You can also use readymade theme elements.
*/
//Print the filter buttons
<div class="concepts-filter button-filters-container">
<div class="button-filter f-all" data-term="all">All</div>
<?php
$terms = get_terms( array(
'taxonomy' => /* [Taxonomy Term Slug] */
'hide_empty' => true,
) );
foreach ($terms as $term) : ?>
<div class="button-filter f-<?php echo $term->term_id ?>" data-term="<?php echo $term->term_id ?>">
<?php echo $term->name ; ?>
</div>
<?php endforeach; ?>
</div>
//Javascript. Can go into a seperate file
<script type="text/javascript">
(function($) {
//On click button filter
$('.button-filter').click(function(){
if(!$(this).hasClass('active-filter')){
var term_id = $(this).data("term");
$(this).siblings().removeClass('active-filter');
$(this).addClass('active-filter');
if(dataterm == 'all'){
$('.item').show();
} else {
$('.item').hide();
$('.item.category-'+term_id).show();
}
} else {
// If already an active filter, do nothing.
}
});
})(jQuery);
</script>
//CSS should go to the relevant stylesheet
<style>
.button-filter {
margin-bottom: 10px;
margin-top: 5px;
background-color: #F5F5F5;
color: #000;
font-weight: bold;
font-size: 16px;
padding: 5px;
cursor: pointer;
margin-right: 25px;
display: inline-block;
padding-right: 30px;
padding-left: 26px;
border-radius: 60px;
}
.active-filter, .button-filter:hover {
background-color: #000 !important;
color: #fff !important;
}
</style>