【问题标题】:Custom wp_query loop pattern自定义 wp_query 循环模式
【发布时间】:2016-09-08 18:19:36
【问题描述】:

这是我试图通过 wp_query 实现的模式。 这将适用于相同的帖子类型。是否可以使用 wp_query 这样做?

谢谢!

        <div>

          <div>1st post from featured</div>

          <div>1st post not from featured</div>

          <div>2nd post from featured</div>

          <div>3rd post from featured</div>

          <div>2nd post not from featured</div>

          <div>3rd post not from featured</div>

          ..and then the same pattern start again with the rest of the posts

          <div>4th post from featured</div>

          <div>4th post not from featured</div>

          <div>5th post from featured</div>

          <div>6th post from featured</div>

          <div>5th post not from featured</div>

          <div>6th post not from featured</div>

          ..until there's no more post
        </div>

【问题讨论】:

    标签: wordpress loops


    【解决方案1】:

    它变得稍微复杂一些。我们需要两个 WP_Query 对象、一些计算和一个 for 循环。

    为了让 PHP 知道正确的格式,我们可以使用一个简单的格式数组。在以下示例中,我将使用类别来确定两个 WP_Query 构建。

    首先,我们构建基础变量:

    <?php 
    
        // Build format
        // 0 means not featured - 1 means featured.
        $format = array(1, 0, 1, 1, 0, 0);
    
        // Retrieve normal posts EXCLUDING category with ID 11 (Featured)
        $normal_posts = new WP_Query(array(
            'post_type'      => 'post',
            'posts_per_page' => 999,
            'cat'            => '-11'
        ));
    
        // Retrieve featured posts ONLY INCLUDING category with ID 11 (Featured)
        $featured_posts = new WP_Query(array(
            'post_type'      => 'post',
            'posts_per_page' => 999,
            'cat'            => 11
        ));
    
        // Calculate total amount of posts
        // from our two WP_Query results
        $posts_total = $normal_posts->post_count + $featured_posts;
    
        // Setup current post index
        $post_index = 0;
    

    接下来,我们开始“循环”或迭代:

        // Iterate every post from two queries
        for($i = 0; $i < $posts_total; $i++){
    

    我们在其中计算当前格式索引:

            // Calculate which type of post to display based on post index
            // We use modulo to get the right $format index
            $post_format_index = $post_index % count($format);
            $current_format    = $format[$post_format_index];
    

    最后,显示正确的帖子类型:

            // Display to frontend based on format
            switch ($current_format) {
                case 1:
                    // Featured
                    $featured_posts->the_post();
    
                    the_title();
    
                    // Your content
    
                    print '<br>';
                    break;
    
                default:
                    // Not Featured
                    $normal_posts->the_post();
    
                    the_title();
    
                    // Your content
    
                    print '<br>';
                    break;
            }
    
            $post_index++;
        }
    
    ?>
    

    就是这样。您可以通过添加另一个 WP_Query 和格式来更进一步。

    $format = array(1, 0, 1, 1, 2, 2);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-03-15
      • 1970-01-01
      • 2020-11-02
      • 2013-09-29
      • 1970-01-01
      • 1970-01-01
      • 2017-08-31
      相关资源
      最近更新 更多