Cth*_*lhu 2 php ajax wordpress jquery
嗨Stack Overflow社区.
我正在为撒旦开发一个wordpress网站.首先,我是一个初级的web开发人员,所以让我说我还有很多需要学习的东西...... :-)这是我第一次使用Ajax,而且我对PHP没有多少经验或word外的SQL.
该网站具有基于复选框的搜索功能.我有2组3个复选框(所以,总共6个).所以,我有2个分类法,每个复选框代表一个不同的术语.我想根据Satan使用这些复选框选择的类别,使用ajax更改页面内容.我能够做到这一点,但一次只能复制一个复选框.因为有几十种不同的组合,我不认为编写几十种不同的查询/参数(每种组合一种)是一个很好的策略.那么,我该怎么做呢?
让我们看一个例子,让你理解我的意思:
想象一下,撒旦想通过一种形式来检查地狱中的灵魂数量.有两组复选框,一组用于人,一组用于惩罚,如下所示:
人
惩罚
现在,我有这个jQuery Ajax代码:
$('#searchsouls').click(function(){
$.ajax({
url:"<?php echo get_bloginfo('template_url'); ?>/evil-laugh.php",
type:"POST",
async:"false",
data:$("#de-form").serialize(),
success:function(data) {
$('#666').empty().html(data);
},
error:function(e){
console.log('Error: ' + e);
}
});
return false;
})
Run Code Online (Sandbox Code Playgroud)
这工作得很好.然后,我在evil-laugh.php里面有这个代码:
<?php global $query_string;
$args = wp_parse_args($query_string);
$args = array(
'post_type' => 'souls',
'paged' => $paged,
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => -1,
'post_status' => 'publish',
);
?>
<!-- THE LOOP -->
<?php $wp_query = new WP_Query( $args ); ?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
Run Code Online (Sandbox Code Playgroud)
当然,这显示了所有的灵魂和各种惩罚,没有任何幻想.我知道我可以通过为每个类别组合定义一组不同的参数来完成撒旦想要的.类似的东西(假设"Impaled"术语的ID为9,而"cat9"是复选框的名称):
if ($_POST['cat9']) {
$args = array(
'post_type' => 'souls',
'paged' => $paged,
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => -1,
'post_status' => 'publish',
'punishment' => 'impaled',
);
Run Code Online (Sandbox Code Playgroud)
但是,有许多不同的组合.以下是一些例子:
我想我可以做某种"for循环"来完成这个,但我不知道如何做到这一点或如何将类别ID添加到wordpress查询,定义相应的AND和OR.
我真的很感激这个问题的一些帮助.但是,请记住,我不想只是一个解决方案,我想了解我正在做什么,并在可能的情况下学习一些新的东西.
提前致谢.
好吧,我自己找到了解决方案.不确定它是否是解决此类问题的最强大的解决方案,但这是为了防止有人在寻找相同的东西.我将每个复选框的名称更改为关联类别的名称,仅出于实际原因.因此,例如,对于我使用的"男人"复选框name="Men",对于其他类别也是如此.这是代码:
if ($_POST['men']) {
$args['people']='men';
}
if ($_POST['women']) {
if ($_POST['men']) {
if ($_POST['children']) {
$args['people']='men, women, children';
} else {
$args['people']='men, women';
}
} else {
$args['people']='women';
}
}
if ($_POST['children']) {
if ($_POST['women']) {
if ($_POST['men']) {
$args['people']='men, women, children';
} else {
$args['people']='women, children';
}
} elseif ($_POST['men']) {
$args['people']='men, children';
} else {
$args['people']='children';
}
}
Run Code Online (Sandbox Code Playgroud)
我对惩罚类别有相同的代码,当然有不同的条款.我相信我的代码仍然有某种冗余,但它运行良好.我将不得不稍后检查.当然这只适用于3个类别,如果你有很多类别,你就不会有这么多有趣的编程,所以最好考虑如何使用"for cycle"或类似的.干杯!
更新:新的更好的解决方案
最后我设法回到这个项目.不仅我设法学习了更多关于PHP的知识,代码现在更加强大,我可以添加尽可能多的类别术语.开始:
$people_args = array(
'type' => 'souls',
'taxonomy' => 'people',
'orderby' => 'name',
'order' => 'ASC',
);
$people = get_categories($people_args);
$arr_people = array();
foreach ($_POST as $key => $value) {
foreach($people as $person){
if($person->slug == $key){
array_push($arr_people, $key);
}
}
}
$values_people = implode(",", $arr_people);
$args['people']=$values_people;
$punishment_args = array(
'type' => 'souls',
'taxonomy' => 'punishment',
'orderby' => 'name',
'order' => 'ASC',
);
$punishments = get_categories($punishment_args);
$arr_punishment = array();
foreach ($_POST as $key => $value) {
foreach($punishments as $punishment){
if($punishment->slug == $key){
array_push($arr_punishment, $key);
}
}
}
$values_punishment = implode(",", $arr_punishment);
$args['punishment']=$values_punishment;
Run Code Online (Sandbox Code Playgroud)
不确定是否有人对此感兴趣,但到目前为止这是我的解决方案.这是一种功能齐全的AJAX方式,可以使用复选框按类别显示内容.一旦网站完成,我会将URL放在这里,以便您可以根据需要测试此代码.知识就是力量.
| 归档时间: |
|
| 查看次数: |
2057 次 |
| 最近记录: |