0

我有一个组织的工作人员的自定义帖子类型,其分类名为 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 的快速回复!我已经尝试在整个循环中使用 counter & % 运算符,但没有成功。我认为它会更符合以下来自易腐新闻的代码行,如下所示......

// 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; ?>

// 第三循环:等 ..................

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

4

1 回答 1

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 -->

希望对你有用!

于 2013-04-09T17:20:24.753 回答