将输出的foreach循环HTML内容输出到单独的列中

时间:2017-01-05 23:14:26

标签: php html wordpress foreach

这是我在Stack上的第一个问题,我已经彻底搜索了答案,但没有成功。我找到了类似的问题,答案很可靠,但遗憾的是我没有抓住任何东西。

我是编程的初学者,最近又加入了WP dev和PHP。我的问题是:

我想找到一种方法来告诉我的代码(这个foreach带有嵌套的Wp_Query循环),在下面列出一个$sklop_cat的X量(在第一个foreach循环中找到)如果到达Y,则将剩余部分列在另一个DIV中。

我已经查看了在循环中使用$subcount = 0方法和++的教程 - 但是我无法弄清楚如何使用嵌套的while循环来做到这一点。< / p>

编辑*

这是我想要的输出,视觉上。当我说我想让我的代码在另一个DIV中列出余数时,如果它到达Y&#39;,我就是DIV元素:

我想要的是什么?

<div class="col-md-4">
/*foreach output*/ 
  <ul>
    <li>/*WP_Query output*/</li>
    <li>/*WP_Query output*/</li>
    <li>/*WP_Query output*/</li>  <-- Once the <li> content being output by the loop reaches 5 then
    <li>/*WP_Query output*/</li>      it stops. Another piece of code then fires in the next <div>
    <li>/*WP_Query output*/</li>
  </ul>
</div>

方法1 - 我当前的代码:

<div class="col-md-4"> 
    <h5> Sub-Categories </h5>   
    <?php

    $subcat_count = 0; //used to keep track of the total number of categories so that we can sort them into two lists.
    $subcat_col_one = []; //used to divide the categories into two columns.
    $subcat_col_two = []; //used to divide the categories into two columns.

    //Extracts ID from slug
    $sklop_id_extractor = get_term_by( 'slug' , $sklop_title_sluggified , 'product_cat');
    $sklop_id_from_slug = $sklop_id_extractor->term_id;
    //Making slug = id
    $parent_cat_ID = $sklop_id_from_slug;
    //Product_cat array 
    $args3 = array(
                   'taxonomy'         => 'product_cat',
                   'hierarchical'     => 1,
                   'show_option_none' => '',
                   'hide_empty'       => 0,
                   'parent'           => $parent_cat_ID
                    );
    $sklop_subcats = get_categories($args3);
                ?>    
    <!--Foreach loop through each sub category + WP_Query through each product within each sub category-->
    <?php
    foreach ($sklop_subcats as $sklop_subcat) { //loops through the subcategories as listed above ^^
    $link = get_term_link( $sklop_subcat->slug, $sklop_subcat->taxonomy );

    echo '<ul class="first_ul">'; 
        echo '<li class="'.$first_li.$indent_1.$li_class.'">';
            echo '<a href="'. $link .'">'.$sklop_subcat->name.'</a>';                                                //echos the subcategory link 
            echo '<ul class="second_ul">';      

            $args4 = array( 'post_type' => 'product', 'posts_per_page' => 5, 'product_cat' => $sklop_subcat->slug );
            $loop = new WP_Query( $args4 );
            while ( $loop->have_posts() ) : $loop->the_post(); 
            //new WP_Query loop to grab the 'Posts' which in this case = 'Products'

              global $product;

              echo '<li class="'.$first_li.$indent_1.$li_class.'"><a class="basic-text" href="'.get_permalink().'">'.get_the_title().'</a></li>';

            endwhile;
            wp_reset_query();
       echo '</ul>';        
    echo '</li>';           
  echo '</ul>';     
  }
  ?>          
  </br><h6><a href='<?php echo site_url().'/product-category/'.$sklop_title_sluggified ?>'>Plus!</a></h6>   
</div>                

<div class="col-md-4">
  <!-- Output column 2 here -->
</div>

方法2

我看到的一种方法几乎有效但我无法正常输出是这样的:

$sklop_id_extractor = get_term_by( 'slug' , $sklop_title_sluggified , 'product_cat');
        $sklop_id_from_slug = $sklop_id_extractor->term_id;

        $parent_cat_ID = $sklop_id_from_slug;
        $args3 = array(
            'taxonomy'         => 'product_cat',
            'hierarchical'     => 1,
            'show_option_none' => '',
            'hide_empty'       => 0,
            'parent'           => $parent_cat_ID
        );

        $sklop_subcats = get_categories($args3);

        //echo '<pre>'; var_dump($categories); echo '</pre>';

        $cat_count = 0; //used to keep track of the total number of categories so that we can sort them into two lists.
        $cat_col_one = []; //used to divide the categories into two columns.
        $cat_col_two = []; //used to divide the categories into two columns.


foreach( $sklop_subcats as $sklop_subcat) {

        $args4 = array( 'post_type' => 'product', 'posts_per_page' => 5, 'product_cat' => $sklop_subcat->slug, 'hide_empty' => 0);
        $loop = new WP_Query( $args4 );
        while ( $loop->have_posts() ) : $loop->the_post(); //new WP_Query loop to grab the 'Posts' which in this case = 'Products'
          global $product;

          $sklop_subcat_product = '<li class="indent_1 left-align HM_li_content"><a class="basic-text" href="'.get_permalink().'">'.get_the_title().'</a></li>';

        endwhile;
        wp_reset_query();

        $cat_count ++;
        $subcat_link_title = sprintf(
        '<ul>'. 
            '<li class="list-unstyled first-li left-align HM_li_content">'.
            '<a href="%1$s" alt="%2$s">%3$s</a></li>'.
                '<ul>'.
                    '<li class="indent_1 list-unstyled first-li left-align HM_li_content">'.
                        ( ! empty($sklop_subcat_product) ? $sklop_subcat_product : '').
                    '</li>'.
                '</ul>'.
        '</ul>' ,
            esc_url( get_category_link( $sklop_subcat->term_id ) ),                                     //%1$s
            esc_attr( sprintf( __('View all posts in %s' , 'textdomain' ) , $sklop_subcat->name ) ),    //%2$s
            esc_html( $sklop_subcat->name )                                                             //%3$s  
        );

        if ($cat_count % 2 != 0) { //Read as: if the cat_count is NOT EVENLY divisible by 2 then do this or that
            $cat_col_one[] = $subcat_link_title;
        } 
        else {
            $cat_col_two[] = $subcat_link_title;
        }
    }

然后在每个元素中使用以下代码进行输出:

<div class="col-md-4">
  foreach($cat_col_one as $cat_one) {
    echo $cat_one;
  }
</div>

<div class="col-md-4">
  foreach($cat_col_two as $cat_two) {
    echo $cat_two;
  }
</div>              

0 个答案:

没有答案