如果不存在值,则删除 URL 查询参数
Remove URL query parameter if no value exists
I found this bit of very useful code 用于生成下拉列表以用作多个自定义分类的 URL 查询字符串。
下面的代码,与 archive.php
文件中 PHP 中的一堆 if(isset($_GET['taxonomies']))
和 if(empty($_GET['taxonomies']))
声明配对,在过滤三个自定义的帖子方面起到了一定的作用分类法。
但是,它仅在 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 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 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 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(); ?>
如果您的查询参数 存在 ,isset()
将 return true
。听起来你的代码假设如果参数存在,它也包含一些有用的值。这不是 isset()
检查的内容。
在这种情况下,the empty()
function 可能就是您要查找的内容。这将 return true
如果一个变量 不 存在或者如果它包含一个 false-y 值,例如你得到的空字符串。
将您的 if
语句更改为 if (!empty($_GET['taxonomies']))
应该可以解决问题。
请记住,字符串 "0"
也被认为是 false-y,因此 "empty",因此如果这是您的查询参数之一的有效值,您需要添加一个显式检查一下。
为了说明@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)
I found this bit of very useful code 用于生成下拉列表以用作多个自定义分类的 URL 查询字符串。
下面的代码,与 archive.php
文件中 PHP 中的一堆 if(isset($_GET['taxonomies']))
和 if(empty($_GET['taxonomies']))
声明配对,在过滤三个自定义的帖子方面起到了一定的作用分类法。
但是,它仅在 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 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 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 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(); ?>
isset()
将 return true
。听起来你的代码假设如果参数存在,它也包含一些有用的值。这不是 isset()
检查的内容。
在这种情况下,the empty()
function 可能就是您要查找的内容。这将 return true
如果一个变量 不 存在或者如果它包含一个 false-y 值,例如你得到的空字符串。
将您的 if
语句更改为 if (!empty($_GET['taxonomies']))
应该可以解决问题。
请记住,字符串 "0"
也被认为是 false-y,因此 "empty",因此如果这是您的查询参数之一的有效值,您需要添加一个显式检查一下。
为了说明@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)