簡體   English   中英

如何限制每個結果頁面上每個類別的 wordpress 搜索結果的數量

[英]How to limit the number of wordpress search results from each category on each result pagel

我正在使用下面的代碼分別顯示不同帖子類型/分段形式的搜索結果。

if (have_posts()) {
    // In the below line of code you can set the loop order in array (1, 2, 3, 4)
    // You can also turn a post type off by removing it from the below line.
    $types = array('post', 'page', 'sfwd-courses', 'product');

    foreach ($types as $type) {
        // Here you can customize both the titles, section heading title & side title
        if ($type == 'post') {
            $head_title = "Results in Posts";
            $side_title = "post";
        }
        if ($type == 'page') {
            $head_title = "Results in Pages";
            $side_title = "page";
        }
        if ($type == 'sfwd-courses') {
            $head_title = "Results in Courses";
            $side_title = "course";
        }
        if ($type == 'product') {
            $head_title = "Results in Products";
            $side_title = "product";
        }

        // This div below shows up the head title for each section
        echo '<div class="search-section"><h4>' . $head_title . '</h4></div>';
        while (have_posts()) {
            the_post();
            if ($type == get_post_type()) {
?>
                <article id="post-<?php the_ID(); ?>" <?php post_class(''); ?> role="article">

                    <header class="article-header">

                        <div class="article-header-content">
                            <h3 class="title head-2"><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
                            <!-- This div below shows up the side title for each section -->
                            <div class="posttypetext"><i> <?php echo $side_title; ?> </i></div>
                        </div>
                        <!-- <?php get_template_part('parts/content', 'byline'); ?> -->
                    </header> <!-- end article header -->

                </article> <!-- end article -->
        <?php }
        }
        rewind_posts();
    }
} else {
    get_template_part('template-parts/content', 'missing');
}

這向我顯示了一個搜索結果列表,首先顯示所有帖子,然后是頁面結果,然后是課程,最后是產品。

現在的分頁限制是每頁 20 個。

我希望在每個頁面上我應該從帖子中獲得5搜索結果,從頁面獲得 5 個搜索結果, 5 5課程和5產品。

這是必要的,因為如果有 30-40 個帖子結果,它將覆蓋搜索結果的前 2 頁,用戶將看不到任何課程或產品將位於第 3、4 頁上。

我希望這是有道理的,有沒有辦法做到這一點?

謝謝 JS

有多種設置方法。 最簡單的方法是利用查詢的current_post屬性,它會為您提供當前帖子的索引號。

因此,在您的代碼中,您可以替換這一行:

if( $type == get_post_type()){
 // your template section
}

有了這條線:

global $wp_query;
if( $type == get_post_type() && $wp_query->current_post <= 4){
 // your template section
}

它是$wp_query->current_post <= 4因為current_post是從零開始的,您只需要 output 前 5 個帖子!

還有其他方法可以使用wp_query來實現相同的目的,但是您沒有提供有關如何創建查詢的任何詳細信息。 您剛剛向我們提供了您的loop模板,我們不知道它是如何被查詢的以及該循環的位置。 所以我認為在這里使用current_post屬性是最簡單的方法!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM