我使用下面的代码从我的wordpress搜索结果中排除一个类别。
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
query_posts( "s=$s&paged=$paged&cat=-35" );但我没有得到结果的最后分页。我应该如何修改search.php以显示分页和排除类别?分页在搜索结果中有效,如果删除我提到的两行,但我需要它们排除类别。
下面是赞赏的search.php code...any帮助。
<?php
/**
* The template for displaying search results pages.
*
* @link https://developer.wordpress.org/themes/basics/template-hierarchy/#search-result
*
*
*/
get_header(); ?>
<div class="container">
<div class="row">
<?php
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
query_posts( "s=$s&paged=$paged&cat=-35" );
$archive_content_classes = apply_filters( 'islemag_archive_content_classes', array( 'islemag-content-left', 'col-md-9' ) );
?>
<div
<?php
if ( ! empty( $archive_content_classes ) ) {
echo 'class="' . implode( ' ', $archive_content_classes ) . '"'; }
?>
>
<div class="post-section islemag-template1">
<div class=" islemag-template1-posts smaller-nav no-radius">
<?php
echo '<div style="font-size:50px;">
Results</div>';
while ( $wp_query->have_posts() ) :
$wp_query->the_post();
$choosed_color = array_rand( $colors, 1 );
$category = get_the_category();
$postid = get_the_ID();
?>
<div class="col-md-3 col-xs-6">
<div class="panel panel-default">
<div class="panel-image text-left">
<figure>
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail();?></a>
</figure> <!-- End figure -->
</div>
<div class="panel-body text-left">
<span class="panel-shopname">
<a style="color:grey;" href="<?php the_permalink(); ?>">
<?php the_field('add_your_brand'); ?>
</a>
</span><a href="<?php the_permalink();?>"><h4 class="panel-promotitle"><?php the_title(); ?></h4></a>
</div>
<div class="panel-footer">
<div class="left-cell promo-price">
<?php the_field('product_price'); ?>
</div>
<div class="right-cell">
<a href="<?php the_permalink(); ?>" class="btn btn-sm btn-success
">Go <i class="fa fa-external-link-square" aria-hidden="true"></i></a>
</div>
</div>
</div>
</div>
<?php
endwhile;
?>
</div>
</div>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>发布于 2018-10-18 07:19:20
您应该将下面的代码粘贴到活动主题的function.php,以排除类别。
function wcs_exclude_category_search( $query ) {
if ( is_admin() || ! $query->is_main_query() )
return;
if ( $query->is_search ) {
$query->set( 'cat', '-35' );
$query->set('paged', ( get_query_var('paged') ) ? get_query_var('paged') : 1 );
$query->set('posts_per_page',6);
}
}
add_action( 'pre_get_posts', 'wcs_exclude_category_search', 1 );并在代码中的the_posts_navigation();后面添加endwhile;。
删除the_posts_navigation();并添加下面的代码
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages,
'before_page_number' => '<span class="screen-reader-text">'.$translated.' </span>'
) );https://stackoverflow.com/questions/52862564
复制相似问题