使用下拉菜单按帖子类型过滤帖子



我想按帖子类型过滤帖子。 因此,我创建了一个下拉菜单,可以在其中选择一个或多个帖子类型。 我现在的问题是:如何获取下拉列表的功能,因为当元素位于前端时,我不知道如何与数据库响应。

这是我的下拉菜单

使用ajax你可以过滤帖子。

这是我按类别过滤帖子的代码(按多个类别选择过滤帖子(

阿贾克斯代码

<script type="text/javascript">
$('.categorytag').click(function(){
var cokeValue = //you need to pass array of post category id 
var data = {
action: 'get_partiblog',
foo:    cokeValue
};
$.post('/wp-admin/admin-ajax.php',data,function(response){
$('#filteredblog').html(response); 
});
})
</script>

PHP代码

<?php
add_action('wp_ajax_get_partiblog','Getblogcatwise');
add_action('wp_ajax_nopriv_get_partiblog','Getblogcatwise');
function Getblogcatwise(){
$catids = $_POST['foo'];
foreach($catids as $catid){
$args = array(
'post_type'=> 'post',
'orderby'    => 'ID',
'category_name' => $catid,
'post_status' => 'publish',
'order'    => 'DESC',
'posts_per_page' =>-1 // this will retrive all the post that is published 
);
$result = new WP_Query( $args );
if ( $result-> have_posts() ) : 
while ( $result->have_posts() ) : $result->the_post(); 
$link = get_permalink();
echo '<a href="'.$link.'" target="_blank">';
echo '<div class="service-wrapper" style="border:1px solid #367bae;">';
/* if ( has_post_thumbnail() ){
echo '<div><img src="'.the_post_thumbnail("thumbnail").'"  alt="Image Missing" rel="lightbox"></div>';
}else{
echo '<div><img src="https://uae.microless.com/cdn/no_image.jpg" alt="" rel="lightbox"></div>';
}*/
$title= get_the_title();
echo '<center><div style="color:#367bae;font-size:22px;font-weight:700;position: relative;margin-top: 25%;padding:5%;">'.$title.'</div></center>';
echo '<div class="service-wrapper-inner">';
//$title= get_the_title();
//echo '<h3>'.$title.'</h3>';
echo '<center><i style="font-size:18px;color:#fff;-webkit-transition: 1s;-moz-transition: 1s;-ms-transition: 1s;-o-transition: 1s; transition: 1s;" class="fa fa-bars"></i></center>';
$excerpt= the_excerpt();
echo '<div class="description"><p>'.$excerpt.'</p></div></div></div></a>';
endwhile; 
endif; wp_reset_postdata();
}
}
?>

最新更新