【问题标题】:Check if Querystrings are not visible on url检查查询字符串是否在 url 上不可见
【发布时间】: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


【解决方案1】:

试试这个:

 if ($_GET){
        if (isset($_GET['category']) && isset($_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 (isset($_GET['category'])) {
            $args = array(
                'numberposts'   => -1,
                'post_type'     => 'job_order',
                'meta_key'      => 'j_category',
                'meta_value'    => $_GET['category']
                );
        } elseif (isset($_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 就会被执行。

并且你应该在获取之前检查该值是否已设置。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-14
    • 1970-01-01
    • 2017-06-12
    • 1970-01-01
    • 2011-05-14
    • 2016-11-20
    • 1970-01-01
    • 2014-01-18
    相关资源
    最近更新 更多