我的本地系统上有wordpress网站,并且有marketify-child主题,其中搜索项目的搜索框,一切正常,但目前通过帖子名称搜索,我想按标签搜索,我做了它也是主要问题,当我有标签可用时,它会给出正确的结果,如果没有结果,那么它将重定向到404.php,我希望保持在同一页面上没有可用的项目消息。< / strong>
在我的搜索框页面代码
下面 <form role="search" method="get" class="search-form<?php echo '' != get_search_query() ? ' active' : ''; ?>" action="<?php echo esc_url( home_url( '/' ) ); ?>">
<button type="submit" class="search-submit"><i class="icon-search"></i></button>
<label>
<span class="screen-reader-text"><?php _ex( 'Search for:', 'label', 'marketify' ); ?></span>
<input type="search" class="search-field" placeholder="<?php echo esc_attr__( 'Search', 'marketify' ); ?>" value="<?php echo esc_attr( get_search_query() ); ?>" name="download_tag" title="<?php echo esc_attr__( 'Search for:', 'marketify' ); ?>">
</label>
<a href="#" class="header-search-toggle"><i class="icon-cross"></i></a>
<!-- <input type="hidden" name="post_type" value="download" /> -->
</form>
在Function.php代码下面 function get_edd_downloads_query($ atts,$ content = null){
$atts = shortcode_atts( array(
'category' => '',
'exclude_category' => '',
'tags' => '',
'exclude_tags' => '',
'relation' => 'OR',
'number' => 9,
'price' => 'no',
'excerpt' => 'yes',
'full_content' => 'no',
'buy_button' => 'yes',
'columns' => 3,
'thumbnails' => 'true',
'orderby' => 'post_date',
'order' => 'DESC',
'ids' => ''
), $atts, 'downloads' );
$query = array(
'post_type' => 'download',
'posts_per_page' => (int) $atts['number'],
'orderby' => $atts['orderby'],
'order' => $atts['order']
);
if ( $query['posts_per_page'] < -1 ) {
$query['posts_per_page'] = abs( $query['posts_per_page'] );
}
switch ( $atts['orderby'] ) {
case 'price':
$atts['orderby'] = 'meta_value';
$query['meta_key'] = 'edd_price';
$query['orderby'] = 'meta_value_num';
break;
case 'title':
$query['orderby'] = 'title';
break;
case 'id':
$query['orderby'] = 'ID';
break;
case 'random':
$query['orderby'] = 'rand';
break;
default:
$query['orderby'] = 'post_date';
break;
}
if ( $atts['tags'] || $atts['category'] || $atts['exclude_category'] || $atts['exclude_tags'] ) {
$query['tax_query'] = array(
'relation' => $atts['relation']
);
if ( $atts['tags'] ) {
$tag_list = explode( ',', $atts['tags'] );
foreach( $tag_list as $tag ) {
if( is_numeric( $tag ) ) {
$term_id = $tag;
} else {
$term = get_term_by( 'slug', $tag, 'download_tag' );
if( ! $term ) {
continue;
}
$term_id = $term->term_id;
}
$query['tax_query'][] = array(
'taxonomy' => 'download_tag',
'field' => 'term_id',
'terms' => $term_id
);
}
}
if ( $atts['category'] ) {
$categories = explode( ',', $atts['category'] );
foreach( $categories as $category ) {
if( is_numeric( $category ) ) {
$term_id = $category;
} else {
$term = get_term_by( 'slug', $category, 'download_category' );
if( ! $term ) {
continue;
}
$term_id = $term->term_id;
}
$query['tax_query'][] = array(
'taxonomy' => 'download_category',
'field' => 'term_id',
'terms' => $term_id,
);
}
}
if ( $atts['exclude_category'] ) {
$categories = explode( ',', $atts['exclude_category'] );
foreach( $categories as $category ) {
if( is_numeric( $category ) ) {
$term_id = $category;
} else {
$term = get_term_by( 'slug', $category, 'download_category' );
if( ! $term ) {
continue;
}
$term_id = $term->term_id;
}
$query['tax_query'][] = array(
'taxonomy' => 'download_category',
'field' => 'term_id',
'terms' => $term_id,
'operator' => 'NOT IN'
);
}
}
if ( $atts['exclude_tags'] ) {
$tag_list = explode( ',', $atts['exclude_tags'] );
foreach( $tag_list as $tag ) {
if( is_numeric( $tag ) ) {
$term_id = $tag;
} else {
$term = get_term_by( 'slug', $tag, 'download_tag' );
if( ! $term ) {
continue;
}
$term_id = $term->term_id;
}
$query['tax_query'][] = array(
'taxonomy' => 'download_tag',
'field' => 'term_id',
'terms' => $term_id,
'operator' => 'NOT IN'
);
}
}
}
if ( $atts['exclude_tags'] || $atts['exclude_category'] ) {
$query['tax_query']['relation'] = 'AND';
}
if( ! empty( $atts['ids'] ) )
$query['post__in'] = explode( ',', $atts['ids'] );
if ( get_query_var( 'paged' ) )
$query['paged'] = get_query_var('paged');
else if ( get_query_var( 'page' ) )
$query['paged'] = get_query_var( 'page' );
else
$query['paged'] = 1;
switch( intval( $atts['columns'] ) ) :
case 0:
$column_width = 'inherit'; break;
case 1:
$column_width = '100%'; break;
case 2:
$column_width = '50%'; break;
case 3:
$column_width = '33%'; break;
case 4:
$column_width = '25%'; break;
case 5:
$column_width = '20%'; break;
case 6:
$column_width = '16.6%'; break;
default:
$column_width = '33%'; break;
endswitch;
// Allow the query to be manipulated by other plugins
$query = apply_filters( 'edd_downloads_query', $query, $atts );
$downloads = new WP_Query( $query );
/*echo "<pre>";
print_r($downloads);die;*/
if ( $downloads->have_posts() ) :
$i = 1;
$wrapper_class = 'edd_download_columns_' . $atts['columns'];
ob_start(); ?>
<div class="custom_edd_downloads_list list_downloads">
<?php while ( $downloads->have_posts() ) : $downloads->the_post(); ?>
<?php $schema = edd_add_schema_microdata() ? 'itemscope itemtype="http://schema.org/Product" ' : ''; ?>
<article class="item">
<div <?php echo $schema; ?>class="<?php echo apply_filters( 'edd_download_class', 'edd_download', get_the_ID(), $atts, $i ); ?>" id="edd_download_<?php echo get_the_ID(); ?>" style="">
<div class="edd_download_inner">
<?php
do_action( 'edd_download_before' );
if ( 'false' != $atts['thumbnails'] ) :
edd_get_template_part( 'shortcode', 'content-image' );
do_action( 'edd_download_after_thumbnail' );
endif;
// edd_get_template_part( 'shortcode', 'content-title' );
// do_action( 'edd_download_after_title' );
if ( $atts['excerpt'] == 'yes' && $atts['full_content'] != 'yes' ) {
edd_get_template_part( 'shortcode', 'content-excerpt' );
do_action( 'edd_download_after_content' );
} else if ( $atts['full_content'] == 'yes' ) {
edd_get_template_part( 'shortcode', 'content-full' );
do_action( 'edd_download_after_content' );
}
if ( $atts['price'] == 'yes' ) {
edd_get_template_part( 'shortcode', 'content-price' );
do_action( 'edd_download_after_price' );
}
if ( $atts['buy_button'] == 'yes' )
edd_get_template_part( 'shortcode', 'content-cart-button' );
do_action( 'edd_download_after' );
?>
</div>
</div>
</article>
<?php if ( $atts['columns'] != 0 && $i % $atts['columns'] == 0 ) { ?><div style="clear:both;"></div><?php } ?>
<?php $i++; endwhile; ?>
</div>
<div style="clear:both;"></div>
<?php wp_reset_postdata(); ?>
<div id="custom_edd_download_pagination" class="navigation">
<?php
if ( is_single() ) {
echo paginate_links( apply_filters( 'edd_download_pagination_args', array(
'base' => get_permalink() . '%#%',
'format' => '?paged=%#%',
'current' => max( 1, $query['paged'] ),
'total' => $downloads->max_num_pages
), $atts, $downloads, $query ) );
} else {
$big = 999999;
$search_for = array( $big, '#038;' );
$replace_with = array( '%#%', '&' );
echo paginate_links( apply_filters( 'edd_download_pagination_args', array(
'base' => str_replace( $search_for, $replace_with, get_pagenum_link( $big ) ),
'format' => '?paged=%#%',
'current' => max( 1, $query['paged'] ),
'total' => $downloads->max_num_pages
), $atts, $downloads, $query ) );
}
?>
</div>
<script type="text/javascript"text/javascript">
jQuery(document).ready(function() {
/*var $list_downloads = jQuery('.list_downloads');
$list_downloads.imagesLoaded(function() {
$list_downloads.masonry({
itemSelector: '.item'
});
});*/
jQuery(function($) {
var $container = jQuery('.content-area');
$container.imagesLoaded(function(){
$container.masonry({
itemSelector: '.item',
isAnimated: true,
animationOptions: { //add animations if you want
duration: 900,
easing: 'easeInOutExpo'
}
});
});
var $container = $('.list_downloads');
$container.infinitescroll ({
navSelector : '#custom_edd_download_pagination', // selector for the paged navigation
nextSelector : '#custom_edd_download_pagination a.page-numbers', // selector for the NEXT link (to page 2)
itemSelector : '.list_downloads .item',
extraScrollPx: 500,
loading: {
finished: undefined,
//loadingText : "Loading new posts...",
loadingText : "",
//finishedMsg: 'No more pages to load.',
finishedMsg: '',
img: '<?php echo get_stylesheet_directory_uri() ;?>/images/ajax-loader.gif',
selector: null,
speed: 'slow',
start: undefined
}
},
//trigger Masonry as a callback
function( newElements ) {
/*function disableScroll() {
if (window.addEventListener) // older FF
window.addEventListener('DOMMouseScroll', preventDefault, false);
window.onwheel = preventDefault; // modern standard
window.onmousewheel = document.onmousewheel = preventDefault; // older browsers, IE
window.ontouchmove = preventDefault; // mobile
document.onkeydown = preventDefaultForScrollKeys;
}*/
$container.imagesLoaded(function(){
$container.masonry({
itemSelector: '.item',
isAnimated: true,
animationOptions: { //add animations if you want
duration: 900,
easing: 'easeInOutExpo'
}
});
});
var $newElems = $( newElements ).css({
opacity: 0
});
$newElems.animate({ opacity: 1 });
$container.masonry( 'appended', $newElems, true );
}
);
});
})
</script>
<style>
/*div#infscr-loading {
text-align: center;
width: 100%;
position: fixed;
}*/
#custom_edd_download_pagination{
display:block;
visibility: hidden;
}
</style>
<?php
$display = ob_get_clean();
else:
$display = sprintf( _x( 'No %s found', 'download post type name', 'edd' ), edd_get_label_plural() );
endif;
return apply_filters( 'downloads_shortcode', $display, $atts, $atts['buy_button'], $atts['columns'], $column_width, $downloads, $atts['excerpt'], $atts['full_content'], $atts['price'], $atts['thumbnails'], $query );
}
add_shortcode( 'custom_downloads', 'get_edd_downloads_query' );
显示结果页面如下
<section id="primary" class="content-area col-md-10 col-sm-9 col-xs-12">
<main id="main" class="site-main" role="main">
<!--
<div class="section-title"><span>
<?php if ( is_search() ) : ?>
<?php //printf( '"%s"', esc_attr( get_search_query() ) ); ?>
<?php else : ?>
<?php //marketify_downloads_section_title(); ?>
<?php endif; ?>
</span></div>
-->
<?php
//echo "string";die;
echo do_shortcode( sprintf( '[custom_downloads number="%s"]', get_option( 'posts_per_page' ) ) ); ?>
</main><!-- #main -->
</section><!-- #primary -->