[英]Using PHP to filter Posts by Category
我正在 Wordpress 中建立一个站点,其中包含可以分类为不同城市(类别)的事件(帖子)列表。
我的目标是能够按它们出现的城市(类别)过滤这些事件(帖子)。
如何按类别过滤事件?
任何帮助将不胜感激,谢谢。
Wordpress 分配给事件帖子的类别
Event = cat4
City A = cat6
City B = cat5
Uncategorized = cat7
<div class="row justify-content-center row-padding" id="brands">
<div class="col-9">
<div class="row justify-content-center">
<div class="col-10">
<div class="animatedParent animateOnce">
<?php if( get_field('brands_title') ):?>
<h1 class="mb-3 animated fadeInUpShort"><span><?php the_field('events_title');?></span></h1>
<?php endif;?>
<?php if( get_field('brands_intro') ):?>
<h4 class="mb-5 animated fadeInUpShort"><?php the_field('events_intro');?></h4>
<?php endif;?>
</div>
</div>
<div class="col-1">
</div>
<div class="col-1 align-self-center animatedParent animateOnce" style="z-index:99;">
<div class="dropdown animated fadeInUpShort">
<button class="btn filter-button dropdown-toggle" type="button" id="dropdownMenuButton1" data-bs-toggle="dropdown" aria-expanded="false">
Filter by City
</button>
<ul class="dropdown-menu" aria-labelledby="dropdownMenuButton1">
<li><a class="dropdown-item" href="#">City A</a></li>
<li><a class="dropdown-item" href="#">City B</a></li>
<li><a class="dropdown-item" href="#">Uncategorized</a></li>
</ul>
</div>
</div>
</div>
<div class="row justify-content-center animatedParent animateOnce" data-sequence="100">
<?php
$brandPosts = new WP_Query('cat=4&posts_per_page=99');
if ($brandPosts->have_posts()) {
$i=1;
while ($brandPosts->have_posts()) {
$brandPosts->the_post();
?>
<div class="col-4 animated fadeIn mb-5 pb-3" data-id="<?php echo $i; ?>">
<div class="row justify-content-center">
<div class="col-12 text-center">
<a href="<?php the_permalink(); ?>"><img alt="<?php the_title(); ?>" src="<?php the_post_thumbnail_url(); ?>" class="img-fluid rounded-circle mb-3"></a>
</div>
</div>
<div class="row">
<div class="col-10 offset-2">
<a href="<?php the_permalink(); ?>"><h2 class="mb-0"><?php the_title(); ?></h2></a>
<p><?php echo excerpt(16); ?></p>
<a href="<?php the_permalink(); ?>" class="see-more">See More</a>
</div>
</div>
</div>
<?php
$i++;
}
} else {
//echo 'No content found';
}
wp_reset_postdata();
?>
</div>
</div>
</div>
为什么不创建自定义 WordPress 模板,例如 country-posts-archive.php 并使用所需的过滤器实现自定义发布查询。
$categories_args = array(
'orderby' => 'menu_order', //prefered order
'taxonomy' => 'product_cat', // the taxonomy we want to get terms of
'exclude' => '17, 69', //exclude something
);
然后在 wp post 循环中使用它,你也可以在 orderby.php 中玩耍
或者,简单的非编码方式只使用侧插件,例如:
https://wordpress.org/plugins/blog-designer/
你也可以用一些棘手的方式来做,用指向存档页面的链接填充下拉列表,例如
www.website.com/category/your-category
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.