使用复选框通过元值获取帖子 [英] Get Posts by Meta Values using checkbox

查看:24
本文介绍了使用复选框通过元值获取帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 wordpress 主题中,我在侧边栏中为自定义字段值创建了两个复选框.苹果和诺基亚是关键品牌的价值所在.

现在我需要在我选中一个框或多个框时显示所有相关帖子?

<块引用>

这是输入代码,

<input type="checkbox" name="mobile" value="nokia"<?php$args = 数组 ('meta_query' =>大批(大批('键' =>'牌','价值' =>'诺基亚'),),);$query = new WP_Query( $args );?>/>诺基亚 <br><input type="checkbox" name="mobile" value="apple"<?php$args = 数组 ('meta_query' =>大批(大批('键' =>'牌','价值' =>'苹果'),),);?>/>苹果 <br></表单>

此查询用于获取帖子,如何在模板文件中使用它来显示复选框结果?

<?php $query = new WP_Query( $args );如果 ( $query->have_posts() ) {while ( $query->have_posts() ) {$query->the_post();get_template_part('内容');}} 别的 {}wp_reset_postdata();?>

解决方案

代码如下

这应该在你的模板中

 <input type="checkbox" name="t[]" value="Nokia" class="br"><input type="checkbox" name="t[]" value="Sony" class="br"><div class="mobile_brand">

</表单><script type="text/javascript">jQuery(文档).ready(函数(){jQuery('.br').click(function(){jQuery('.contents').remove();var 检查 = jQuery('#test').serialize();jQuery.ajax({url:"<?php echo admin_url('admin-ajax.php'); ?>",数据:"action=call_post&"+checked,成功:功能(对象){var render_data = "

";//这是为了观察你的 json 对象控制台日志(对象);for(var i=0;i<obj.length;i++){console.log(obj[i].post_title);render_data+="<h4>"+obj[i].post_title+"</h4>";render_data+="<p>"+obj[i].post_content+"</p>";}render_data+="</div>";jQuery(render_data).appendTo('.mobile_brand');}});})});

这应该在你的 functions.php

add_action('wp_ajax_call_post','call_post');add_action('wp_ajax_nopriv_call_post','call_post');函数 call_post(){$test = $_REQUEST['手机'];$args = 数组('post_type' =>'邮政','meta_query' =>大批(大批('键' =>'牌','价值' =>$测试,//'比较' =>'在',),),);$query = new WP_Query( $args );wp_send_json($query->posts);}

这只是给您的示例代码.您需要通过迭代 JSON 来呈现数据.

In my wordpress theme i create two checkbox in sidebar for custom field value. Apple and Nokia are values for key name Brand.

Now i need when i check a box or multiple box it show all relative post?

Here is input code,

<form>
<input type="checkbox" name="mobile" value="nokia"
<?php  
$args = array (
'meta_query'    => array(
    array(
        'key'   => 'brand',
        'value' => 'nokia'  
    ),
 ),
);
$query = new WP_Query( $args );
?>/> Nokia <br>


<input type="checkbox" name="mobile" value="apple"
<?php  
$args = array (
'meta_query'    => array(
    array(
        'key'   => 'brand',
        'value' => 'apple'  
    ),
 ),
);
?>/> Apple <br>
</form>

This query is used to get post, How this is used in template file for showing checkbox result?

<?php $query = new WP_Query( $args );
     if ( $query->have_posts() ) {
 while ( $query->have_posts() ) {
    $query->the_post();
   get_template_part('content');
 }
} else {
}
wp_reset_postdata();
?>

解决方案

Here is the code

this should be in your template

    <form id='test'>
<input type="checkbox" name="t[]" value="Nokia" class="br">
<input type="checkbox" name="t[]" value="Sony" class="br">
<div class="mobile_brand">

</div>
</form>
<script type="text/javascript">
    jQuery(document).ready(function(){
        jQuery('.br').click(function(){
            jQuery('.contents').remove();
            var checked = jQuery('#test').serialize();
            jQuery.ajax({
                url:"<?php echo admin_url('admin-ajax.php'); ?>",
                data:"action=call_post&"+checked,
                success:function(obj){
                     var render_data = "<div class='contents'>";

                    // This is to watch your json object
                    console.log(obj);   

                    for(var i=0;i<obj.length;i++)
                    {
                        console.log(obj[i].post_title);
                        render_data+="<h4>"+obj[i].post_title+"</h4>";
                        render_data+="<p>"+obj[i].post_content+"</p>";
                    }
                    render_data+="</div>";
                    jQuery(render_data).appendTo('.mobile_brand');

                }
            });
        })
    });
</script>

This should be in your functions.php

add_action('wp_ajax_call_post','call_post');
add_action('wp_ajax_nopriv_call_post','call_post');
function call_post(){
    $test = $_REQUEST['mobile'];

    $args = array(
        'post_type'  => 'post',
        'meta_query' => array(
            array(
                'key'     => 'brand',
                'value'   => $test,
                // 'compare' => 'IN',
            ),
        ),
    );

$query = new WP_Query( $args );
wp_send_json($query->posts);

}

This is just sample code for you. You need to render data by iterating JSON.

这篇关于使用复选框通过元值获取帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆