【发布时间】:2016-12-21 05:28:45
【问题描述】:
我有这段代码来检查查询字符串是否存在并根据值回显内容。
如果满足以下条件,代码正在运行:
www.mywebsite.com/?position=&category=&country=
www.mywebsite.com/?position=position1&category=&country=
www.mywebsite.com/?position=&category=category1&country=
www.mywebsite.com/?position=&category=&country=country1
www.mywebsite.com/?position=&category=category1&country=country
.
.
and so forth.
但如果 url 只是 www.mywebsite.com 而没有查询字符串,则不会执行父 else 块。
我当前的代码是:
<?php
// args
if ($_GET){
if ($_GET['category'] && $_GET['country']) {
$args = array(
'numberposts' => -1,
'post_type' => 'job_order',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'j_category',
'value' => $_GET['category'],
'compare' => '='
),
array(
'key' => 'j_country',
'value' => $_GET['country'],
'compare' => '='
)
)
);
} elseif ($_GET['category']) {
$args = array(
'numberposts' => -1,
'post_type' => 'job_order',
'meta_key' => 'j_category',
'meta_value' => $_GET['category']
);
} elseif ($_GET['country']) {
$args = array(
'numberposts' => -1,
'post_type' => 'job_order',
'meta_key' => 'j_country',
'meta_value' => $_GET['country']
);
} else {
$args = array(
'numberposts' => -1,
'post_type' => 'job_order'
);
}
} else{
$args = array(
'numberposts' => -1,
'post_type' => 'job_order'
);
}
// query
$the_query = new WP_Query( $args );
if( $the_query->have_posts() ):
while( $the_query->have_posts() ) : $the_query->the_post();
?>
【问题讨论】:
-
并且您没有在代码中寻找位置,因此与问题无关。
标签: php wordpress if-statement get query-string