Wordpress中复杂的自定义帖子类型多个循环

时间:2013-04-09 07:50:07

标签: wordpress loops

我有一个名为Profession的分类的组织的员工的自定义帖子类型。我正在使用easytabs为每个分类显示不同职业的成员显示照片矩阵。当用户点击照片(标签导航)时,相应的信息会显示在面板中,因为它可以动画显示。

我只能在页面上的每个标签容器div中容纳4个成员,而第5个成员会打破标签布局。

我需要一个循环,每个容器只能拉4个工作人员,然后接下来的4个等等。

这是我到目前为止的代码......

<div class="team_content">
            <?php
                $custom_terms = get_terms('profession');

                foreach($custom_terms as $custom_term) {
                    wp_reset_query();
                        $args = array('post_type' => 'team_members',
                            'tax_query' => array(
                                array(
                                    'taxonomy' => 'profession',
                                    'field' => 'slug',
                                    'terms' => $custom_term->slug,
                                ),
                            ),
                         );

                         $loop = new WP_Query($args);
                         if($loop->have_posts()) {
                            echo '<h2>'.$custom_term->name.'</h2>'; //displays the profession

                            echo '<div class="tab-collapsible-container">';
                                echo '<ul>';

                            while($loop->have_posts()) : $loop->the_post(); //first sub-loop

                                //extract field names from metaboxes
                                $salutation = get_post_meta( $post->ID, '_cmb_salutation', true );
                                $title = $salutation.' '.get_the_title();
                                $full_title = get_the_title();
                                $title_link = str_replace(' ','',$full_title); 
                                $final_title_link = strtolower($title_link);

                                echo '<li><a href="#'.$final_title_link.'">';
                                the_post_thumbnail("team-member");
                                echo '<h4>'.$title.'</h4></a></li>';

                            endwhile;

                                echo '</ul>';

                            rewind_posts();

                            echo '<div class="panel-container">';

                            while($loop->have_posts()) : $loop->the_post();

                                //extract field names from metaboxes
                                $profession = get_post_meta( $post->ID, '_cmb_profession', true );
                                $qualifications = get_post_meta( $post->ID, '_cmb_qualifications', true );
                                $services_url = get_post_meta( $post->ID, '_cmb_services_url', true );
                                $full_title2 = get_the_title();
                                $title_link2 = str_replace(' ','',$full_title2); 
                                $final_title_link2 = strtolower($title_link2);

                                echo '<div id="'.$final_title_link2.'" class="member_info">';
                                    echo '<h4>'.$profession.' '.$qualifications.'</h4>';
                                    the_content();
                                    echo '<a href="'.$services_url.'" class="button2">Visit Service Page</a>'; //Services page link
                                echo '</div>';

                            endwhile;

                            echo '</div>'; //panel-container

                            }

                        echo '</div>'; //tab-collapsible-container

                        }
                    ?>



        </div><!-- .team_content -->

谢谢@ anstrangel0ver的快速回复!我试过计数器&amp;遍布循环的%运算符没有成功。我认为它将更加符合perishable press的以下代码,如下所示......

// FIRST LOOP: display posts 1 thru 5
<?php query_posts('showposts=5'); ?>
<?php $posts = get_posts('numberposts=5&offset=0'); foreach ($posts as $post) :     start_wp(); ?>
<?php static $count1 = 0; if ($count1 == "5") { break; } else { ?>

<?php the_title(); ?>
<?php the_content(); ?>

<?php $count1++; } ?>
<?php endforeach; ?>


// SECOND LOOP: display posts 6 thru 10
<?php query_posts('showposts=5'); ?>
<?php $posts = get_posts('numberposts=5&offset=5'); foreach ($posts as $post) : start_wp(); ?>
<?php static $count2 = 0; if ($count2 == "5") { break; } else { ?>

<?php the_title(); ?>
<?php the_content(); ?>

<?php $count2++; } ?>
<?php endforeach; ?>

//第三圈:等等     ..................          

只是不确定如何将此应用于我的循环并保持一切正确。

1 个答案:

答案 0 :(得分:0)

哎呀,这是rewind_posts()那里的混乱商业。首先,我将创建一个函数,可以通过一定数量的帖子推进您的自定义WP_Query:

function offset_posts($loop, $offset) {
    $post_counter = 0;
    while ($loop->have_posts() && ($post_counter < $offset) {
        $loop->next_post();
        $post_counter++;
    }
}

然后我会将你的员工成员输出代码拉成一个可以一次编写一个由四名员工组成的块的函数:

function output_staff_members($loop, $offset, $number_to_output = 4) {
    // skip posts until we get to $offset
    offset_posts($loop, $offset);

    $post_counter = 0;
    while ($loop->have_posts() && ($post_counter < $number_to_output)) :  //first sub-loop
        $loop->the_post();
        $post_counter++;

        //extract field names from metaboxes
        $salutation = get_post_meta( $post->ID, '_cmb_salutation', true );
        $title = $salutation.' '.get_the_title();
        $full_title = get_the_title();
        $title_link = str_replace(' ','',$full_title); 
        $final_title_link = strtolower($title_link);

        echo '<li><a href="#'.$final_title_link.'">';
        the_post_thumbnail("team-member");
        echo '<h4>'.$title.'</h4></a></li>';

    endwhile;

    echo '</ul>';

    $loop->rewind_posts();
    offset_posts($loop, $offset);

    echo '<div class="panel-container">';

    $post_counter = 0;
    while ($loop->have_posts() && ($post_counter < $number_to_output)) :  //second sub-loop
        $loop->the_post();
        $post_counter++;

        //extract field names from metaboxes
        $profession = get_post_meta( $post->ID, '_cmb_profession', true );
        $qualifications = get_post_meta( $post->ID, '_cmb_qualifications', true );
        $services_url = get_post_meta( $post->ID, '_cmb_services_url', true );
        $full_title2 = get_the_title();
        $title_link2 = str_replace(' ','',$full_title2); 
        $final_title_link2 = strtolower($title_link2);

        echo '<div id="'.$final_title_link2.'" class="member_info">';
            echo '<h4>'.$profession.' '.$qualifications.'</h4>';
            the_content();
            echo '<a href="'.$services_url.'" class="button2">Visit Service Page</a>'; //Services page link
        echo '</div>';

    endwhile;

    echo '</div>'; //panel-container

    return $post_counter;
}

最后,你只需要足够多次调用新功能来输出所有工作人员:

<div class="team_content">
        <?php
            $custom_terms = get_terms('profession');

            foreach($custom_terms as $custom_term) {
                wp_reset_query();
                    $args = array('post_type' => 'team_members',
                        'tax_query' => array(
                            array(
                                'taxonomy' => 'profession',
                                'field' => 'slug',
                                'terms' => $custom_term->slug,
                            ),
                        ),
                     );

                     $loop = new WP_Query($args);
                     if($loop->have_posts()) {
                        echo '<h2>'.$custom_term->name.'</h2>'; //displays the profession

                        echo '<div class="tab-collapsible-container">';
                        echo '<ul>';

                        // NEW CODE HERE:
                        $post_counter = 0;
                        while ($post_counter < $loop->$post_count) {
                            $post_counter += output_staff_members($loop, $post_counter);
                        }

                     }

                     echo '</div>'; //tab-collapsible-container

                 }
            ?>



    </div><!-- .team_content -->

希望对你有用!