如何分配一个唯一的类在循环中返回的每个项目? [英] How to Assign a unique class to each item returned in a loop?

查看:129
本文介绍了如何分配一个唯一的类在循环中返回的每个项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让PHP一些帮助,所以我可以一个类或ID添加到一个循环返回的每个单独的项目。我使用的是一块code(见下文)返回分类(位置)。我有我的所有的32个完美的回来,我的网站上显示。但是,我不能单独作为有与他们每个人的关联没有独特的类样式通过CSS的每个位置。

返回的每一块看起来像这样在前端

 < D​​IV CLASS =块>
&所述; A HREF =htt​​p://example.com/location/new-york>
    < H2>
        &所述; A HREF =htt​​p://example.com/location/new-york>
        安特里姆
        < BR>
        <跨度>(1农夫)LT; / SPAN>
        &所述; / A>
    < / H>

这将重复所有地点的回报。正如你可以看到就没有办法单独其实每个样式而这正是我需要。

我的code返回的位置如下。我曾尝试使用Google和几个不同的方式来做到这一点,只是觉得我出我的联赛,我想我会问专家这里一些帮助!

感谢

 < PHP
全球$ woo_options;
$ post_types_to_loop_through =阵列();
$ posts_to_exclude =阵列();
$ categories_panel_entries = $ woo_options ['woo_categories_panel_entries'];
$ more_listings_setting = $ woo_options ['woo_more_listings_area'];
$ wp_custom_post_types_args =阵列();
$ wp_custom_post_types = get_post_types($ wp_custom_post_types_args,对象);
的foreach($ wp_custom_post_types为$ post_type_item){
$ cpt_test = get_option('woo_categories_panel_post_types _'$ post_type_item->名);
如果($ cpt_test =='真'){
    $ cpt_nice_name = $ post_type_item-> labels->名;
    $ cpt_has_archive = $ post_type_item-> has_​​archive;
    $ post_types_to_loop_through [$ post_type_item->名称] =阵列('nice_name'=> $ cpt_nice_name,'has_archive'=> $ cpt_has_archive);
}
}$ section_counter = 0;的foreach($ post_types_to_loop_through为$ post_type_item => $ post_type_item_nice_name){
$ =分类阵列(位置);
$ block_counter = 0;
的$ args =阵列('排序依据'=>'名','为了'=>'ASC');
?>< D​​IV ID =位置>    < PHP
    //新的和改进查询
    $ all_terms = get_terms($分类,$参数);    $ block_counter = 0;
    的foreach($ all_terms为$ all_term){        $ tax_test = get_option('woo_categories_panel_taxonomies _'$ all_term-方式>分类法);        如果(($ tax_test =='真')及及($ block_counter&下; $ categories_panel_entries)){            $ post_images =阵列();
            $ posts_aray =阵列();            $ term_name = $ all_term->名;
            $ term_slug = $ all_term->蛞蝓;
            $ term_id = $ all_term-> term_id;
            $ term_link = get_term_link($ all_term,$ all_term->分类法);
            $ counter_value = $ all_term->计数;
            ?>
            < D​​IV CLASS =块>
                < A HREF =< PHP的echo $ term_link;>中>
                < PHP
                    $ block_counter ++;                    如果($ POST_ID大于0){                        //如果一个有特色的图像是可用的,用它优先于形象领域。
                        如果(function_exists('has_post_thumbnail)及和放大器; current_theme_supports(后缩略图)){                            如果(has_post_thumbnail($ POST_ID)){                                $ _id = 0;
                                $ _id = get_post_thumbnail_id($ POST_ID);                                如果(INTVAL($ _id)){                                    $ _image =阵列();
                                    $ _image = wp_get_attachment_image_src($ _id,'全');                                    // $ _image应该有3个指标:URL,宽度和高度。
                                    如果(计数($ _image)){                                        $ _image_url = $ _image [0];                                        woo_image('SRC ='$ _image_url'和;键=图像和放大器; WIDTH = 139安培;高度= 81安培;链接= IMG');                                    } // END IF语句                                } // END IF语句                            }其他{                                woo_image('ID ='$ POST_ID'和;键=图像和放大器; WIDTH = 139安培;高度= 81安培;链接= IMG');                            } // END IF语句                        }其他{                            woo_image('ID ='$ POST_ID'和;键=图像和放大器; WIDTH = 139安培;高度= 81安培;链接= IMG');                        } // END IF语句                    }其他{
                        // 倒退
                        woo_taxonomy_image($ post_images,$ term_link);                    } // END IF语句
                    $ php_formatting =M \\ / D \\ / Y;
                    $ post_item_date =的strtotime($ post_date_raw);
                ?>
                    &所述; / A>
                < H2>< A HREF =< PHP的echo $ term_link;>中><?PHP的echo $ term_name> < BR /><跨度>(小于?PHP的echo $ counter_value;>农民)LT; / SPAN>< / A>< / H>
                < P>< PHP _e('最新上市','woothemes')><?PHP的回声日期($ php_formatting,$ post_item_date); ?>&下; / P>
            < / DIV><! - /.block - >
            < PHP
            如果($ block_counter%5 == 0){
            ?>
                < D​​IV CLASS =修复>< / DIV>
            < PHP
            } // END IF语句        } // END IF语句        ?>
        < PHP
    } //结束For循环    ?>    < D​​IV CLASS =修复>< / DIV>    &所述; PHP如果($ block_counter大于0)?{>
    < PHP}其他{&GT?;
    < p类=呜 - SC-对话框注意的风格=保证金:20像素20像素;>< PHP _e('请添加一些职位,以便此面板正常工作,必须指定岗位?这些分类为它工作,woothemes')。 ?>&下; / P>
    < PHP}&GT?;< / DIV><! - /.listings - >< D​​IV CLASS =修复>< / DIV>

更新
由于一些帮助,我已经实现了这个code,但仍无法得到它的工作!

我已将此添加到全局变量上线8
$ I = 0; //初始化变量递增

然后我加入这只是在div之前为u可以使用额外的类我想呼应见

  //我说这条线作为我的div之前只是建议
< PHP的foreach($ all_terms为$ all_term){&GT?;//我再添加一个新的类,但我不知道该怎么写
< D​​IV CLASS =块级=< PHP回声后$ I;?>>

刚刚闭幕div标签之前(大约100行后)我添加结束code

  $ I ++; //增加它
}


解决方案

由于@sushanth雷迪说,这确实应该为

一样简单

  $ I = 0; //初始化变量递增
的foreach($ all_terms为$ all_term){
    //你的code的休息;如果你希望你的递增ID /类中使用$ I    $ I ++; //增加它
}

I am trying to get some help in PHP so I can add a class or ID to each individual item returned in a loop. I am using a piece of code (see below) to return taxonomies (locations). I have all my 32 locations returned perfectly and displayed on my site. However, I cannot individually style each location via css as there is no unique class associated with each of them.

Each block that is returned looks like this on the frontend

<div class="block">
<a href="http://example.com/location/new-york">
    <h2>
        <a href="http://example.com/location/new-york">
        Antrim
        <br>
        <span>(1 Farmer)</span>
        </a>
    </h2>

which repeats for all locations returns. As you can see there would be no way to actually style each individually which is what I require.

My code which returns the locations is as follows. I have tried googling and a few different ways to do it, but feel I am out of my league and thought I would ask the experts here for some help!

Thanks

<?php
global $woo_options;
$post_types_to_loop_through = array();
$posts_to_exclude = array();
$categories_panel_entries = $woo_options['woo_categories_panel_entries'];
$more_listings_setting = $woo_options['woo_more_listings_area'];
$wp_custom_post_types_args = array();
$wp_custom_post_types = get_post_types($wp_custom_post_types_args,'objects');
foreach ($wp_custom_post_types as $post_type_item) {
$cpt_test = get_option('woo_categories_panel_post_types_'.$post_type_item->name);
if ($cpt_test == 'true') {
    $cpt_nice_name = $post_type_item->labels->name;
    $cpt_has_archive = $post_type_item->has_archive;
    $post_types_to_loop_through[$post_type_item->name] = array('nice_name' => $cpt_nice_name, 'has_archive' => $cpt_has_archive);
}
}

$section_counter = 0;

foreach ($post_types_to_loop_through as $post_type_item => $post_type_item_nice_name) {
$taxonomies = array('location');
$block_counter = 0;
$args = array('orderby' => 'name', 'order' => 'ASC');
?>

<div id="location">

    <?php 
    // NEW AND IMPROVED QUERY
    $all_terms = get_terms( $taxonomies, $args );

    $block_counter = 0;
    foreach ( $all_terms as $all_term) {

        $tax_test = get_option('woo_categories_panel_taxonomies_'.$all_term->taxonomy);

        if ( ($tax_test == 'true') && ($block_counter < $categories_panel_entries) ) {

            $post_images = array();
            $posts_aray = array();

            $term_name = $all_term->name;
            $term_slug = $all_term->slug;
            $term_id = $all_term->term_id;
            $term_link = get_term_link( $all_term, $all_term->taxonomy );
            $counter_value = $all_term->count;
            ?>
            <div class="block">
                <a href="<?php echo $term_link; ?>">
                <?php
                    $block_counter++;



                    if ( $post_id > 0 ) {

                        // If a featured image is available, use it in priority over the "image" field.
                        if ( function_exists( 'has_post_thumbnail' ) && current_theme_supports( 'post-thumbnails' ) ) {

                            if ( has_post_thumbnail( $post_id ) ) {

                                $_id = 0;
                                $_id = get_post_thumbnail_id( $post_id );

                                if ( intval( $_id ) ) {

                                    $_image = array();
                                    $_image = wp_get_attachment_image_src( $_id, 'full' );

                                    // $_image should have 3 indexes: url, width and height.
                                    if ( count( $_image ) ) {

                                        $_image_url = $_image[0];

                                        woo_image('src=' . $_image_url . '&key=image&width=139&height=81&link=img');

                                    } // End IF Statement

                                } // End IF Statement

                            } else {

                                woo_image('id='.$post_id.'&key=image&width=139&height=81&link=img');

                            } // End IF Statement

                        } else {

                            woo_image('id='.$post_id.'&key=image&width=139&height=81&link=img');

                        } // End IF Statement

                    } else {
                        // Fallback
                        woo_taxonomy_image($post_images,$term_link);

                    } // End IF Statement
                    $php_formatting = "m\/d\/Y";
                    $post_item_date = strtotime($post_date_raw);
                ?>
                    </a>
                <h2><a href="<?php echo $term_link; ?>"><?php echo $term_name ?> <br/><span>(<?php echo $counter_value; ?> Farmers)</span></a></h2>
                <p><?php _e('Latest listing ', 'woothemes') ?><?php echo date($php_formatting,$post_item_date); ?></p>
            </div><!-- /.block -->
            <?php
            if ( $block_counter % 5 == 0 ) {
            ?>
                <div class="fix"></div>
            <?php
            } // End IF Statement   

        } // End IF Statement

        ?>
        <?php


    } // End For Loop

    ?>

    <div class="fix"></div>

    <?php if ( $block_counter > 0 ) { ?>




    <?php } else { ?>    
    <p class="woo-sc-box note" style="margin:20px 20px;"><?php _e('Please add some posts in order for this panel to work correctly. You must assign the post to these taxonomies for it to work.','woothemes'); ?></p>
    <?php } ?>

</div><!-- /.listings -->

<div class="fix"></div>

Update Thanks to some of the help I have implemented this code, but still can't get it to work!

I added this to the global variables on line 8 $i = 0; // initialise incrementing variable

I then added this just before the div as u can see with the extra class I am trying to echo

// I added this line as suggested just before my div
<?php foreach ( $all_terms as $all_term) { ?>

// I then added a new class but I was not sure how to write this
<div class="block" class="<?php echo "post-" . $i; ?>">

Just before the closing div tag (about 100 lines later) I add the closing code

 $i++; // increment it
}

解决方案

As @sushanth reddy said, it should really be as simple as

$i = 0; // initialise incrementing variable
foreach ( $all_terms as $all_term) {
    // rest of your code; use $i when you want your incrementing ID / class

    $i++; // increment it
}

这篇关于如何分配一个唯一的类在循环中返回的每个项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆