如果没有值删除网址查询参数

问题描述 投票:0回答:2

found this bit of very useful code产生下拉的URL查询字符串多个自定义分类使用。

下面的代码,在PHP中if(isset($_GET['taxonomies']))文件一堆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(); ?>
php wordpress filter custom-post-type taxonomy
2个回答
2
投票

如果你的查询参数存在isset()将返回true。这听起来像你的代码假设,如果参数存在,它也包含了一些有用的价值。这不是什么isset()检查。

在这种情况下,the empty() function可能是你在找什么。这将返回true如果一个变量不存在,或者如果它包含一个假的y值,如您得到一个空字符串。

更改您的if语句来if (!empty($_GET['taxonomies']))应该做的伎俩。

请记住,字符串"0"也被认为是假-Y,因此“空”,因此,如果这是一个有效的价值为你的查询参数之一,你将需要添加一个明确的检查了点。


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)
© www.soinside.com 2019 - 2024. All rights reserved.