dr5648 2016-08-17 11:40
浏览 43

如何使用数组编程PHP循环?

In a wordpress site for a dental clinic, there are several services (e.g. Orthodontics). These services are like "categories", using both names and slug-names. (e.g. Orthodontics / cat_orthodontics).

In the frontend, the services page shows every category, the name of the category and a description. I just need to put images related to every category.

I noticed that in the category file (category.php) that creates the area to show every one of these images, there are several lines that control the frontend:

<?php   
                    $args = array( 'taxonomy' => 'services_category', 'hide_empty' => '0', 'exclude' => $pixhealth_deps);                           
                    $pixhealth_categories = get_categories ($args);                             
                    if( $pixhealth_categories ):
                        foreach($pixhealth_categories as $pixhealth_cat) :
                            $pixhealth_t_id = $pixhealth_cat->term_id;
                            $pixhealth_cat_meta = get_option("services_category_$pixhealth_t_id");
                            $pixhealth_link = !isset($pixhealth_cat_meta['pix_serv_url']) || $pixhealth_cat_meta['pix_serv_url'] == '' ? get_term_link( $pixhealth_cat ) : $pixhealth_cat_meta['pix_serv_url'];
                ?>                                      
                            <div class="departments-item ">
                                <span class="icon-round bg-color_second helper">
                                    <i class="icon <?php echo esc_attr($pixhealth_cat_meta['pix_icon']) ?>"></i>
                                </span>
                                </div>
                                <h3 class="ui-title-inner"><?php echo wp_kses_post($pixhealth_cat->name) ?></h3>
                                <p class="ui-text"><?php echo pixhealth_limit_words($pixhealth_cat->description, 20) ?></p>
                                <a class="btn btn_small" href="<?php echo esc_url($pixhealth_link) ?>"><?php _e("READ MORE", "PixHealth") ?></a>
                            </div>
                <?php                           
                         endforeach;
                    endif; ?>

The "departments-item" creates every displayed service in the frontend:

<div class="departments-item ">

To put images I need to create a new div for every service and delete the <i class="icon"> because I don't want to show icons for every service. I need images.

So I tried this:

<?php   
                    $args = array( 'taxonomy' => 'services_category', 'hide_empty' => '0', 'exclude' => $pixhealth_deps);                           
                    $pixhealth_categories = get_categories ($args);                             
                    if( $pixhealth_categories ):
                        foreach($pixhealth_categories as $pixhealth_cat) :
                            $pixhealth_t_id = $pixhealth_cat->term_id;
                            $pixhealth_cat_meta = get_option("services_category_$pixhealth_t_id");
                            $pixhealth_link = !isset($pixhealth_cat_meta['pix_serv_url']) || $pixhealth_cat_meta['pix_serv_url'] == '' ? get_term_link( $pixhealth_cat ) : $pixhealth_cat_meta['pix_serv_url'];
                ?>                                      
                            <div class="departments-item ">
                                <!--<span class="icon-round bg-color_second helper">
                                    <i class="icon <?php /*?><?php echo esc_attr($pixhealth_cat_meta['pix_icon']) ?><?php */?>"></i>
                                </span>-->
                                <?php servicios=array('cirugia_oral','endodoncia','estetica_dental','implantes_dentales','odontopediatria','ortodoncia','periodoncia','rehabilitacion_oral');   ?>
                                <div class="inner-departments <?php echo wp_kses_post($pixhealth_cat->name) ?>">

                                </div>
                                <h3 class="ui-title-inner"><?php echo wp_kses_post($pixhealth_cat->name) ?></h3>
                                <p class="ui-text"><?php echo pixhealth_limit_words($pixhealth_cat->description, 20) ?></p>
                                <a class="btn btn_small" href="<?php echo esc_url($pixhealth_link) ?>"><?php _e("READ MORE", "PixHealth") ?></a>
                            </div>
                <?php                           
                         endforeach;
                    endif; ?>

I added an array:

<?php servicios=array('cirugia_oral','endodoncia','estetica_dental','implantes_dentales','odontopediatria','ortodoncia','periodoncia','rehabilitacion_oral'); ?>

and added a line to create every div:

<div class="inner-departments <?php echo wp_kses_post($pixhealth_cat->name) ?>">

I noticed on another line that: <?php echo wp_kses_post($pixhealth_cat->name) ?> echoes the name of every category, so I decided to use this inside my new div to create every category class.

However, I would like to use the names from the array and not the names from every category because some names have special characters like "ñ" and this cannot be used as a class.

I don't know how to relate the names of the categories to every item in the array, using perhaps a foreach for every item in the array with every name in the categories? The idea is to create a unique class for every category so I can then add images using CSS for every class.

If you have a better idea how I can add unique images to every class let me know.

Thank you for any help.

  • 写回答

2条回答 默认 最新

  • duandingyou3331 2016-08-17 11:44
    关注

    Okay I think I understand what you're trying to do.

    You'll need to add the category names to your array. Code is untested, so you may need to debug, but the idea is there.

    <?php   
        $args = array( 'taxonomy' => 'services_category', 'hide_empty' => '0', 'exclude' => $pixhealth_deps);                           
        $pixhealth_categories = get_categories ($args);    
    
        if( $pixhealth_categories ):
            $servicios=array(
                'category_name1'=>'cirugia_oral',
                'category_name2'=>'endodoncia',
                'category_name3'=>'estetica_dental',
                'category_name4'=>'implantes_dentales',
                'category_name5'=>'odontopediatria',
                'category_name6'=>'ortodoncia',
                'category_name7'=>'periodoncia',
                'category_name8'=>'rehabilitacion_oral');   
    
            foreach($pixhealth_categories as $pixhealth_cat) :
                $pixhealth_t_id = $pixhealth_cat->term_id;
                $pixhealth_cat_meta = get_option("services_category_$pixhealth_t_id");
                $pixhealth_link = !isset($pixhealth_cat_meta['pix_serv_url']) || $pixhealth_cat_meta['pix_serv_url'] == '' ? get_term_link( $pixhealth_cat ) : $pixhealth_cat_meta['pix_serv_url'];
    ?>                                      
                <div class="departments-item ">
                    <!--<span class="icon-round bg-color_second helper">
                        <i class="icon <?php /*?><?php echo esc_attr($pixhealth_cat_meta['pix_icon']) ?><?php */?>"></i>
                    </span>-->
    
                    <div class="inner-departments <?php echo $servicios[$pixhealth_cat->name]; ?>">
                     <!-- div content -->
                    </div>
    
                    <h3 class="ui-title-inner"><?php echo wp_kses_post($pixhealth_cat->name) ?></h3>
                    <p class="ui-text"><?php echo pixhealth_limit_words($pixhealth_cat->description, 20) ?></p>
                    <a class="btn btn_small" href="<?php echo esc_url($pixhealth_link) ?>"><?php _e("READ MORE", "PixHealth") ?></a>
                </div>
    <?php                           
            endforeach;
        endif; ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭