【问题标题】:Limit number of products on WooCommerce search results page限制 WooCommerce 搜索结果页面上的产品数量
【发布时间】:2016-12-10 09:53:43
【问题描述】:

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

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

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

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案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(); ?>
    

    【讨论】:

    • 谢谢。然而它没有帮助(还)。它仍然显示超过设置的数量 (12)。
    • 嗯,当我点击一个标签(在前端)时,它看起来很完美。当我使用小部件 WooCommerce 产品搜索时,它不会.....
    猜你喜欢
    • 2020-12-11
    • 2020-12-03
    • 1970-01-01
    • 2016-08-23
    • 1970-01-01
    • 1970-01-01
    • 2012-09-28
    • 1970-01-01
    • 2018-09-29
    相关资源
    最近更新 更多