如果不存在值,则删除URL查询参数

时间:2019-02-08 23:41:22

标签: php wordpress filter custom-post-type taxonomy

found this bit of very useful code用于生成一个下拉列表,以用作多个自定义分类法的URL查询字符串。

下面的代码与if(isset($_GET['taxonomies']))文件中的PHP中的if(empty($_GET['taxonomies']))archive.php声明配对,可以过滤三种自定义分类法中的信息。

但是,它仅在URL查询参数不为空时才有效。例如:

example.com/custom-post-type-slug/?taxonomy1=term1&taxonomy2=termA&taxonomy3=termZ

...工作正常。

example.com/custom-post-type-slug/?taxonomy1=term1&taxonomy2=termA

...也可以。

但是,下面的代码创建的下拉列表将生成一个URL查询字符串,其中包含空查询参数,例如:

example.com/custom-post-type-slug/?taxonomy1=term1&taxonomy2=&taxonomy3=termZ

分类法2的空查询参数将其破坏。缺少所有查询参数:

example.com/custom-post-type-slug/?taxonomy1=&taxonomy2=&taxonomy3=

如果没有值,如何调整下面的代码以删除URL查询参数?

最理想的情况是,如果没有URL查询值,则从字符串中删除查询参数。如何使用此代码执行此操作?

这是在我的functions.php文件中。

function get_terms_dropdown_taxonomy1($taxonomies, $args){
            $myterms = get_terms($taxonomies, $args);
            $output ="<select name='taxonomy1'>";
            $output .="<option value=''>All Taxonomy 1</option>";
            foreach($myterms as $term){
                    $root_url = get_bloginfo('url');
                    $term_taxonomy=$term->taxonomy;
                    $term_slug=$term->slug;
                    $term_name =$term->name;
                    $link = $term_slug;
                    $output .="<option value='".$link."'>".$term_name."</option>";
            }
            $output .="</select>";
    return $output;
    }

function get_terms_dropdown_taxonomy2($taxonomies, $args){
            $myterms = get_terms($taxonomies, $args);
            $output ="<select name='taxonomy2'>";
            $output .="<option value=''>All Taxonomy 2</option>";
            foreach($myterms as $term){
                    $root_url = get_bloginfo('url');
                    $term_taxonomy=$term->taxonomy;
                    $term_slug=$term->slug;
                    $term_name =$term->name;
                    $link = $term_slug;
                    $output .="<option value='".$link."'>".$term_name."</option>";
            }
            $output .="</select>";
    return $output;
    }

function get_terms_dropdown_taxonomy3($taxonomies, $args){
            $myterms = get_terms($taxonomies, $args);
            $output ="<select name='taxonomy3'>";
            $output .="<option value=''>All Taxonomy 3</option>";
            foreach($myterms as $term){
                    $root_url = get_bloginfo('url');
                    $term_taxonomy=$term->taxonomy;
                    $term_slug=$term->slug;
                    $term_name =$term->name;
                    $link = $term_slug;
                    $output .="<option value='".$link."'>".$term_name."</option>";
            }
            $output .="</select>";
    return $output;
    }

同时,这是在模板文件中输出的。

        <?php
            $taxonomies = array('taxonomy1');
            $args = array('orderby'=>'name','hide_empty'=>1);
            $select = get_terms_dropdown_taxonomy1($taxonomies, $args);
            $select = preg_replace("#<select([^>]*)>#", "<select$1 id='taxonomy1' class='dropdown' aria-label='Select a taxonomy 1'>", $select);
            echo $select;
        ?>

        <?php
            $taxonomies = array('taxonomy2');
            $args = array('orderby'=>'name','hide_empty'=>1);
            $select = get_terms_dropdown_taxonomy2($taxonomies, $args);
            $select = preg_replace("#<select([^>]*)>#", "<select$1 id='taxonomy2' class='dropdown' aria-label='Select a taxonomy 2'>", $select);
            echo $select;
        ?>

        <?php
            $taxonomies = array('taxonomy3');
            $args = array('orderby'=>'name','hide_empty'=>1);
            $select = get_terms_dropdown_taxonomy3($taxonomies, $args);
            $select = preg_replace("#<select([^>]*)>#", "<select$1 id='taxonomy3' class='dropdown' aria-label='Select a taxonomy 3'>", $select);
            echo $select;
        ?>

编辑:感谢您的帮助,这里是最终的工作代码。

<?php
$url = parse_url($_SERVER['REQUEST_URI']);

    // WORKING | all taxonomy1, taxonomy2, and taxonomy3; no values for any taxonomy; displays six most recent posts

    if(empty($_GET['taxonomy1']) && empty($_GET['taxonomy2']) && empty($_GET['taxonomy3'])) {

    $paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1; $exec_query = new WP_Query( array (
        'post_type'         => 'custom_post_type',
        'publish_status'    => 'publish',
        'posts_per_page'    => 6,
        'paged'             => $paged,
    ) );
    }

    // WORKING | taxonomy1 only; no values for taxonomy2 or taxonomy3

    elseif(empty($_GET['taxonomy2']) && empty($_GET['taxonomy3'])) {

    $paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1; $exec_query = new WP_Query( array (
        'post_type'         => 'custom_post_type',
        'publish_status'    => 'publish',
        'posts_per_page'    => 6,
        'paged'             => $paged,
                    'tax_query'         => array(
                        array(
                            'taxonomy'  => 'taxonomy1',
                            'field'     => 'slug',
                            'terms'     => $_GET['taxonomy1'],
                            'operator'  => 'IN',
                        ),
                    ),
    ) );
    }

    // WORKING | issue only; no values for taxonomy1 or taxonomy3; displays six most recent taxonomy2 posts

    elseif(empty($_GET['taxonomy1']) && empty($_GET['taxonomy3'])) {

    $paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1; $exec_query = new WP_Query( array (
        'post_type'         => 'custom_post_type',
        'publish_status'    => 'publish',
        'posts_per_page'    => 6,
        'paged'             => $paged,
                    'tax_query'         => array(
                        array(
                            'taxonomy'  => 'taxonomy2',
                            'field'     => 'slug',
                            'terms'     => $_GET['taxonomy2'],
                            'operator'  => 'IN',
                        ),
                    ),
    ) );
    }

    // WORKING | taxonomy3 only; no values for taxonomy1 or taxonomy2

    elseif(empty($_GET['taxonomy1']) && empty($_GET['taxonomy2'])) {

    $paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1; $exec_query = new WP_Query( array (
        'post_type'         => 'custom_post_type',
        'publish_status'    => 'publish',
        'posts_per_page'    => 6,
        'paged'             => $paged,
                    'tax_query'         => array(
                        array(
                            'taxonomy'  => 'taxonomy3',
                            'field'     => 'slug',
                            'terms'     => $_GET['taxonomy3'],
                            'operator'  => 'IN',
                        ),
                    ),
    ) );
    }   

    // WORKING | taxonomy2 in taxonomy3; no values for taxonomy1

    elseif(empty($_GET['taxonomy1'])) {

    $paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1; $exec_query = new WP_Query( array (
        'post_type'         => 'custom_post_type',
        'publish_status'    => 'publish',
        'posts_per_page'    => 6,
        'paged'             => $paged,
                    'tax_query'         => array(
                        'relation'      => 'AND',
                        array(
                            'taxonomy'  => 'taxonomy2',
                            'field'     => 'slug',
                            'terms'     => $_GET['taxonomy2'],
                            'operator'  => 'IN',
                        ),
                        array(
                            'taxonomy'  => 'taxonomy3',
                            'field'     => 'slug',
                            'terms'     => $_GET['taxonomy3'],
                            'operator'  => 'IN',
                        ),
                    ),
    ) );
    }

    // WORKING | taxonomy1 in taxonomy3; no values for taxonomy2

    elseif(empty($_GET['taxonomy2'])) {

    $paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1; $exec_query = new WP_Query( array (
        'post_type'         => 'custom_post_type',
        'publish_status'    => 'publish',
        'posts_per_page'    => 6,
        'paged'             => $paged,
                    'tax_query'         => array(
                        'relation'      => 'AND',
                        array(
                            'taxonomy'  => 'taxonomy1',
                            'field'     => 'slug',
                            'terms'     => $_GET['taxonomy1'],
                            'operator'  => 'IN',
                        ),
                        array(
                            'taxonomy'  => 'taxonomy3',
                            'field'     => 'slug',
                            'terms'     => $_GET['taxonomy3'],
                            'operator'  => 'IN',
                        ),
                    ),
    ) );
    }

    // WORKING | taxonomy1 on taxonomy2; no values for taxonomy3

    elseif(empty($_GET['taxonomy3'])) {

    $paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1; $exec_query = new WP_Query( array (
        'post_type'         => 'custom_post_type',
        'publish_status'    => 'publish',
        'posts_per_page'    => 6,
        'paged'             => $paged,
                    'tax_query'         => array(
                        'relation'      => 'AND',
                        array(
                            'taxonomy'  => 'taxonomy1',
                            'field'     => 'slug',
                            'terms'     => $_GET['taxonomy1'],
                            'operator'  => 'IN',
                        ),
                        array(
                            'taxonomy'  => 'taxonomy2',
                            'field'     => 'slug',
                            'terms'     => $_GET['taxonomy2'],
                            'operator'  => 'IN',
                        ),
                    ),
    ) );
    }

elseif(isset($_GET['taxonomy1']) && ($_GET['taxonomy2']) && ($_GET['taxonomy3'])) {

$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1; $exec_query = new WP_Query( array (
    'post_type'         => 'custom_post_type',
    'publish_status'    => 'publish',
    'posts_per_page'    => 6,
    'paged'             => $paged,
                'tax_query'         => array(
                    'relation'      => 'AND',
                    array(
                        'taxonomy'  => 'taxonomy1',
                        'field'     => 'slug',
                        'terms'     => $_GET['taxonomy1'],
                        'operator'  => 'IN',
                    ),
                    array(
                        'taxonomy'  => 'taxonomy2',
                        'field'     => 'slug',
                        'terms'     => $_GET['taxonomy2'],
                        'operator'  => 'IN',
                    ),
                    array(
                        'taxonomy'  => 'taxonomy3',
                        'field'     => 'slug',
                        'terms'     => $_GET['taxonomy3'],
                        'operator'  => 'IN',
                    ),
                ),
) );
}

    else{

    $paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1; $exec_query = new WP_Query( array (
        'post_type'         => 'custom_post_type',
        'publish_status'    => 'publish',
        'posts_per_page'    => 6,
        'paged'             => $paged,
    ) );
    }

    if ( $exec_query->have_posts() ) { ?><?php while ( $exec_query->have_posts() ): $exec_query->the_post(); ?>

2 个答案:

答案 0 :(得分:2)

如果查询参数存在

isset()将返回true。听起来您的代码假设如果该参数存在,它也包含一些有用的值。 isset()不会检查。

在这种情况下,the empty() function可能就是您想要的。如果变量不存在或者它包含false-y值(例如您要获取的空字符串),则会返回true

if语句更改为if (!empty($_GET['taxonomies']))应该可以解决问题。

请记住,字符串"0"也被认为是false-y,因此也被认为是“空”,因此,如果这是您的查询参数之一的有效值,则需要对此进行显式检查。

答案 1 :(得分:0)

为说明@rickdenhaan所说的内容,下面是一些示例:

// Check if $variable is empty, using empty($variable)

$variable = ""; // true
$variable = " "; // false
$variable = 0; // true
$variable = 1; // false
$variable = null; // true
$variable = false; // true

// Check if $variable was defined, using isset($variable)

$variable = ""; // true
$variable = " "; // true
$variable = 0; // true
$variable = 1; // true
$variable = null; // false
$variable = false; // true

好奇心: 如文档中所述,empty()函数实际上是:

(!isset($variable) || $variable == false)