限制WooCommerce搜索结果页面上的产品数量

时间:2016-12-10 09:53:44

标签: php wordpress woocommerce

在我的functions.php中,我有以下代码行:

// Display 48 products per page.
add_filter( 'loop_shop_per_page', create_function( '$cols', 'return 48;' ), 20 );

这限制了商店页面和类别页面上显示的产品数量。但是,当我在产品标签上搜索时,这在结果页面上不起作用。我在这里错了什么?

1 个答案:

答案 0 :(得分:1)

这将改变产品搜索结果的限制。我跳这将对您有所帮助。

function searchfilter($query) {

    if ($query->is_search && !is_admin() ) {
        $query->set('post_type',array('product'));
        $query->set('posts_per_page',25);
    }

return $query;
}

add_filter('pre_get_posts','searchfilter');

案例2:创建自己的

要自定义搜索结果模板,请在主题中创建一个名为search.php的新文件,该文件仅用于搜索页面。

<?php
/**
 * Search Results Template File
 */
get_header(); ?>
    <header>
        <h1>Search Results: &quot;<?php echo get_search_query(); ?>&quot;</h1>
        <br>
    </header>
<?php if ( have_posts() ) :  // results found?>
    <?php while ( have_posts() ) : the_post(); ?>
        <article>
            <h2><?php the_title();  ?></h2>
            <p><?php the_excerpt(); ?></p>
            <p> <a href="<?php the_permalink(); ?>">View</a> </p>
        </article>
    <?php endwhile; ?>
<?php else :  // no results?>
    <article>
        <h1>No Results Found.</h1>
    </article>
<?php endif; ?>
<?php get_footer(); ?>
相关问题