在WordPress中将X个分类帖子放在单独的行中

时间:2017-04-03 作者:Anahit DEV

我正在尝试创建一个带有分类法帖子类别及其帖子的滑块,因此结构如下所示

Row 1
    First Category Title
      First category Post Title Goes Here
    Second Category Title
      Second category Post Title Goes Here
Row 2
    Third Category Title
      Third category Post Title Goes Here
    Fourth Category Title
      Fourth category Post Title Goes Here
因此,我目前尝试获取分类法类别及其帖子的内容如下

<div class="clientsListRow">
                    <?php 
                        $post_type = \'clients\';
                        $taxonomies = get_object_taxonomies(array( \'post_type\' => $post_type));
                        foreach($taxonomies as $taxonomy) :
                            $terms = get_terms($taxonomy);
                            foreach($terms as $term) : ?>
                              <ul><li><a><?php echo $term->name; ?></a>
                                <?php
                                $args = array(
                                    \'post_type\' => $post_type,
                                    \'posts_per_page\' => -1,  //show all posts
                                    \'tax_query\' => array(
                                        array(
                                            \'taxonomy\' => $taxonomy,
                                            \'field\' => \'slug\',
                                            \'terms\' => $term->slug,
                                        )
                                    )
                                );
                                $posts = new WP_Query($args);
                                if($posts->have_posts()): ?>
                                <ul>
                                <?php  while($posts->have_posts() ) : $posts->the_post(); ?>
                                    <li><a><?php  echo get_the_title(); ?></a></li>
                                <?php endwhile; endif; ?>
                                </ul></li></ul>
                            <?php endforeach;
                        endforeach; 
                    ?>
                </div>
但无法创建带有div的分离部分,因此我在div中用X个ul量将每行包装起来。

你知道我是如何做到这一点的吗?

1 个回复
SO网友:WebElaine

这应该会让你离得很近。关键是使用迭代循环并检查正在进行的迭代,以确定是否输出新的<div class="row">, 打开或关闭<ul>, 等

请注意,您处于循环中的一个the_permalink()the_title() 因为查询已经抓到了它们,所以不需要echo get_the_title()

<div class="clientsListRow"><?php 
    $post_type = \'clients\';
    $taxonomies = get_object_taxonomies(array(\'post_type\' => $post_type));
    foreach($taxonomies as $taxonomy) :
        $terms = get_terms($taxonomy);
        for($t=1; $t<=count($terms); $t++) {
            // if "t" is 1 or a multiple of 7, start a new row
            if($t == 1 || $t%7 == 0) { ?><div class="row"><?php }
                // if "t" is 1 or a multiple of 4, start a new list
                if($t == 1 || $t%4 == 0) { ?><ul><?php } ?>
                <li>
                    <a href="<?php echo get_term_link($term->term_id); ?>"><?php echo $term->name; ?></a>
                    <?php
                    $args = array(
                        \'post_type\' => $post_type,
                        \'posts_per_page\' => -1,  //show all posts
                        \'tax_query\' => array(
                            array(
                                \'taxonomy\' => $taxonomy,
                                \'field\' => \'slug\',
                                \'terms\' => $term->slug,
                            )
                        )
                    );
                    $posts = new WP_Query($args);
                    if($posts->have_posts()) : ?>
                        <ul>
                        <?php while($posts->have_posts() ) : $posts->the_post(); ?>
                            <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
                        <?php endwhile; ?>
                        </ul>
                    <?php endif; ?>
                </li>
            <?php // if "t" is a multiple of 3, or the very last item, close the list
            if($t%3 == 0 || $t == count($terms)) { ?></ul><?php
            // if "t" is a multiple of 6, or the very last item, close the row
            if($t%6 == 0 || $t == count($terms)) { ?></div><!-- end row --><?php
        }
    endforeach; ?>
</div>

相关推荐

如何控制根据Taxonomy术语显示什么模板?

我正在建立一个商业目录,并将给出一些背景,然后在最后有2个问题。The development URL is: http://svcta.lainternet.biz/The website I am rebuilding is: https://www.visitsimivalley.com/当前网站要求每个分类法类型具有唯一的业务概要文件。例如,如果您是一家酒店,并且您也有会议室和婚礼场地,那么您最终会得到3个列表,一个用于酒店,一个用于会议,一个用于婚礼。我希望有一个主配置文件,其中包含我们将显示的