我
found this bit of very useful code
用于生成下拉列表以用作多个自定义分类的URL查询字符串。
下面的代码,与一组
if(isset($_GET['taxonomies']))
和
if(empty($_GET['taxonomies']))
php中的声明
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查询值,则从字符串中删除查询参数。如何使用此代码?
这在my 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(); ?>