调整搜索结果页面分页的结果数量

时间:2020-01-31 作者:Devon

我试图覆盖搜索分页时显示的帖子数量。php模板,16对4。在后端的阅读设置中,它被设置为4,这是需要的,因为我的首页使用相同的代码来显示最近的4篇文章。

有没有办法在搜索中指定此项。php模板页?

这是我的搜索模板页。php

<?php 
    get_header();
    global $wp_query;
?>

<picture class="featured-image block  pos-rel">
    <div class="t-con flex row pos-abs">
        <h1  class="post-title f-center" title="<?php echo $wp_query->found_posts; ?> <?php _e( \'Search Results Found For\', \'locale\' ); ?>: <?php the_search_query(); ?>"><?php echo $wp_query->found_posts; ?> <?php _e( \'Search Results Found For\', \'locale\' ); ?>: "<?php the_search_query(); ?>"</h1> <!--Edit these values to adjust page being shown.-->
    </div>
    <?php echo  get_the_post_thumbnail($page=2, \'full\', array( 
        \'class\' => \'featured\'
    )); ?>
</picture>

<section class="main bg-darkpurple">
<div>
    <div class="grid row posts">
        <?php while ( have_posts() ) : the_post(); ?>
        <div class="col">
            <div class="date-container">
                <p class="post-date bg-darkpurple">
                    <?php 
                        $u_time = get_the_time(\'U\');
                        $u_modified_time = get_the_modified_time(\'U\');
                            if ($u_modified_time >= $u_time + 86400) {
                        echo "Last updated ";
                            the_modified_time(\'F jS, Y\');
                        echo ""; }
                        else {echo "Posted "; the_time(\'F jS, Y\');}
                    ?>
                </p>
            </div>
            <?php
                $primary_cat = get_post_meta($post->ID , \'_category_permalink\', true);
                    if ( $primary_cat != null ) {
                        $cat_id = $primary_cat[\'category\'];
                        $category = get_category($cat_id);
                    } else {
                        $categories = get_the_category();
                        $category = $categories[0];
                    }
                        $category_link = get_category_link($category);
                        $category_name = $category->name;  
                ?>

                <picture class="post-thumb block">

                    <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(\'medium\'); ?></a>
                    <div class="cat-container">
                        <a class="post-cat bg-darkpurple" href="<?php echo $category_link ?>"><?php echo $category_name ?></a>
                    </div>

                </picture>

                <h3>
                    <a href="<?php the_permalink(); ?>">
                        <?php the_title(); ?>
                    </a>
                </h3>
                    <p><?php echo substr(get_the_excerpt(), 0,160); ?></p>
        </div>
        <?php endwhile; ?>
    </div>
        <div class="pagination flex row f-center">
            <?php 
                echo paginate_links( array(
                    \'end_size\'     => 2,
                    \'mid_size\'     => 1,
                    \'prev_text\'    => sprintf( \'<i></i> %1$s\', __( \'Other results\', \'text-domain\' ) ),
                    \'next_text\'    => sprintf( \'%1$s <i></i>\', __( \'Other results\', \'text-domain\' ) ),
                ) );
            ?>
        </div>
    <?php wp_reset_postdata(); ?>
</div>
</section>

<?php get_footer(); ?>
我知道这行代码应该是我所需要的,但我不确定该放在哪里:

\'posts_per_page\' => 16
谢谢你的帮助!

2 个回复
最合适的回答,由SO网友:Charles 整理而成

也许是这个function 会帮你的
添加到functions.php 备份此文件后
无需使用此功能在模板中添加任何内容。

/**
 * Set max output results for search page
 * 
 * @param  $queryVars
 * @return results
 *
 * Read more   @link   https://developer.wordpress.org/reference/classes/requests   /request/
 * Tested with @version 5.3.2
 * 
 */
add_filter( \'request\', \'search_results_pro_page\' );
function search_results_pro_page( $queryVars )
{
    // Prevent access to back-end
    if ( is_admin() ) {
        return;
    }

    // Set only on search
    if ( isset( $_REQUEST[\'s\'] ) ) {        
        // Adjust number of results shown
        $queryVars[\'posts_per_page\'] = 4;  
    }
    // Return amount search results 
    return $queryVars;                      
}

SO网友:Tom J Nowell

如果要修改WordPress从数据库中获取的帖子,请使用pre_get_posts 筛选以在查询进入数据库之前拦截查询

e、 g。

add_action( \'pre_get_posts\', function( WP_Query $query ) {
    // don\'t mess with admin search, just the frontend
    if ( is_admin() {
        return;
    }
    // if it\'s a search query, and it\'s the main query
    if ( $query->is_main_query() && $query->is_search() ) {
        // then change query variables using the set method
        $query->set( \'....\', \'....\' );
        // etc..
    }
} );
因为这是一个钩子,它可以进去functions.php 或插件。加载模板时,查询已经获取了帖子,因此您不会将其放入search.php

相关推荐

JQuery未调用php函数

我似乎无法让jquery在插件中调用php处理函数。请帮忙。TQ公司更新:我更新了代码。它现在正在工作。我的php文件// INIT function nizam_scripts() { wp_enqueue_script( \'nizam_ajax\', plugin_dir_url( __FILE__ ) . \'/nizam_plugin.js\', array( \'jquery\' ) ); wp_localize_script(\'nizam_ajax\