我是个超级新手,所以我会尽力解释。
我有一个博客页面,我想按类别过滤,我希望用户能够点击多个类别,它将过滤以显示这些帖子。
我知道了如何做一个帖子分类,我只是不知道如何保持已经点击的值并添加另一个值。
我现在拥有的
http://localhost/blog/?filter=issues
我想我在找什么
http://localhost/blog/?filter=issues&voices
导航
<form action="/blog" method="get">
<ul class="show">
<li><button name="filter" type="submit" value="all">All</button></li>
<li><button name="filter" type="submit" value="issues" class="active">Issues</button></li>
<li><button name="filter" type="submit" value="members-voice">Member\'s Voice</button></li>
<li><button name="filter" type="submit" value="politics">Politics</button></li>
<li><button name="filter" type="submit" value="voices">Voices</button></li>
</ul>
</form>
如何查询帖子
<?php $query = "";
if ($_GET[\'filter\'] == \'all\') {
$categories = get_categories( $args );
foreach ($categories as $category) {
$fliterArray[] = $category->slug;
$query = implode(",", $fliterArray);
}
} else {
$query = $_GET[\'filter\'];
}
?>
<ul>
<?php $args = array(
\'post_type\' => array(\'slideshow_article\',\'interior\'),
\'category_name\' => $query, // This needs to be either one Categorie or a few Categorie or All Categorie
\'posts_per_page\' => 1000,
\'meta_query\' => array(
array(
\'key\' => \'hide_feed\', // name of custom field
\'value\' => \'0\',
)
),
); ?>
SO网友:user86755
这是我问题的答案
<?php
// If there is a value to filter
if($_GET[\'filter\']) {
// $urlHasFilter = true;
// Create an array out of query string of the filters example (issues,voices)
$urlFilters = explode(\',\', $_GET[\'filter\']);
} else {
// $urlHasFilter = false;
// if filter has nothing create an empty array
$urlFilters = array();
}
$isAll = \'\';
// if filter = all add class active to that link
if($_GET[\'filter\'] == \'all\') {
$isAll = \'active\';
}
?>
<ul>
<li><a href="?filter=all" class="<?php echo $isAll; ?>">All</a></li>
<?php
$args = array(
\'orderby\' => \'name\',
\'parent\' => 0,
// \'exclude\' => 3,
);
$categories = get_categories($args);
// loop through the categorys that are being used.
foreach ( $categories as $category ) {
$class = "";
$active = false;
$urlFiltersMinus = array();
// Loop through the array of query strings
foreach($urlFilters as $urlFilter) {
// Added class "active" if matches that Category slug
if($urlFilter == $category->slug) {
$class = "active";
$active = true;
} else {
$urlFiltersMinus[] = $urlFilter;
}
}
if($active) {
$url = implode(\',\', $urlFiltersMinus);
} else {
$urlFiltersMinus[] = $category->slug;
$url = implode(\',\', $urlFiltersMinus);
}
echo \'<li><a href="?filter=\'.$url.\'"class=\'.$class.\'value="\'.$url.\'">\'.$category->name.\'</a></li>\';
}
?>
</ul>