admin管理员组

文章数量:1122832

I am writing a theme for Wordpress and I need a tag filter tool for posts, that can filter by multiple tags simultaneously, without a submit button, no checkboxes, no radio’s, and a show all posts button. (See image)

What would be the best way to come at this problem? or can anyone recommend any suitable plugins?

I am writing a theme for Wordpress and I need a tag filter tool for posts, that can filter by multiple tags simultaneously, without a submit button, no checkboxes, no radio’s, and a show all posts button. (See image)

What would be the best way to come at this problem? or can anyone recommend any suitable plugins?

Share Improve this question asked Jun 14, 2020 at 13:32 Theo fordTheo ford 11 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

I recently wrote a bit of code which filters by one tag at a time only, but it would be pretty easy to modify it to 'toggle' the status of each tag so that you could filter by multiple tags simultaneously. You can see it working here: http://sadcookbook.recipes/sad/ . It uses the following PHP to render the tag links,

Show only:
<?php
$tags = get_tags();
foreach ($tags as $tag) {
        $taglinks[] = "<span style=\"cursor: pointer;\" class=\"filter-tag\" id=\"filter-".$tag->slug."\"><b>".$tag->name."</b></span>";
}
echo join($taglinks, " - ");`

Then it uses this jquery together with masonry to display posts depending on which tags have been clicked

jQuery('.filter-tag').click(function() {

    showAll();
    var $grid = jQuery('#grid-container');
    tag = 'tag-' + jQuery(this).attr('id').substring(7)
    classSelector = '.' + tag
    elementsToRemove = jQuery('#gridcontainer').children().children().not(classSelector).parent()

    elementsToRemove.clone().appendTo('#grid-hidden')
    $grid.masonry('remove', elementsToRemove)
    $grid.masonry('layout')

    // Save the elements removed so they can be put back later
    elementsToRemove.each(function (index) {
            id = jQuery(this).attr('id');
            if (!putBack.includes(id))
                    putBack.push(id)
    });

});

本文标签: phpMultiple Tag Filtering