【问题标题】:How to program a PHP loop using an array?如何使用数组编写 PHP 循环?
【发布时间】:2016-08-17 11:40:37
【问题描述】:

在牙科诊所的 wordpress 网站中,有多种服务(例如牙齿矫正)。这些服务就像“类别”,使用名称和 slug 名称。 (例如 Orthodontics / cat_orthodontics)。

在前端,服务页面显示每个类别、类别名称和描述。我只需要放置与每个类别相关的图像。

我注意到在创建区域以显示这些图像中的每一个的类别文件 (category.php) 中,有几行控制前端:

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

“departments-item”在前端创建每个显示的服务:

&lt;div class="departments-item "&gt;

要放置图像,我需要为每个服务创建一个新的 div 并删除 &lt;i class="icon"&gt;,因为我不想为每个服务显示图标。我需要图片。

所以我尝试了这个:

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

我添加了一个数组:

&lt;?php servicios=array('cirugia_oral','endodoncia','estetica_dental','implantes_dentales','odontopediatria','ortodoncia','periodoncia','rehabilitacion_oral'); ?&gt;

并添加了一行来创建每个 div:

&lt;div class="inner-departments &lt;?php echo wp_kses_post($pixhealth_cat-&gt;name) ?&gt;"&gt;

我注意到在另一行中:&lt;?php echo wp_kses_post($pixhealth_cat-&gt;name) ?&gt; 与每个类别的名称相呼应,因此我决定在我的新 div 中使用它来创建每个类别类。

但是,我想使用数组中的名称,而不是每个类别中的名称,因为某些名称具有特殊字符,例如“ñ”,并且不能用作类。

我不知道如何将类别的名称与数组中的每个项目相关联,也许为数组中的每个项目使用一个 foreach 以及类别中的每个名称?这个想法是为每个类别创建一个独特的类,这样我就可以使用 CSS 为每个类添加图像。

如果您对如何为每个班级添加独特的图像有更好的想法,请告诉我。

感谢您的帮助。

【问题讨论】:

  • 我认为您需要简化您的要求。
  • 答案已更新,我想这就是您所需要的?
  • 感谢@logic-unit 清理我的问题!

标签: php wordpress class loops


【解决方案1】:

好的,我想我明白你要做什么了。

您需要将类别名称添加到您的数组中。代码未经测试,因此您可能需要调试,但想法就在那里。

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

【讨论】:

  • 感谢您的代码,我尝试了所有方法,但代码不起作用,最后 div 内没有类。可能出了什么问题?
  • 你替换了数组中的数组键类别名称吗?
【解决方案2】:

我看到你的问题来自特殊字符。

我不会在代码中添加任何额外的数组(维护变得更加困难)。

  1. $pixhealth_categories 变量有什么作用?您应该将其打印到屏幕上(例如 print_r( $pixhealth_categories ); )。您可以查找数组中应包含的内容:https://developer.wordpress.org/reference/functions/get_categories/
  2. 我会将 get_categories() 更改为 get_terms()(更多功能的返回值 - 我建议也将其打印出来,链接:https://developer.wordpress.org/reference/functions/get_terms/)。因此,新的 $pixhealth_categories 数组包含更多信息——甚至是类别的 slug 值。 slug 不使用特殊字符,因此可以用作 CSS 类名。
  3. 您的原始代码中有一个小错误:servicios=array('cirugia_oral','endodoncia','estetica_dental','implantes_dentales','odontopediatria','ortodoncia','periodoncia','rehabilitacion_oral '); 应以 $ 符号开头。

所以,我想我会试试这个,然后调试,如果它失败:

<?php   
$args = array( 'taxonomy' => 'services_category', 'hide_empty' => '0', 'exclude' => $pixhealth_deps);                           
$pixhealth_categories = get_terms($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->slug) ?>">

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

我的建议中的基本思想是将 get_categories() 更改为 get_terms() - 然后一切都可以根据需要放置到位。 :)

然后有一个“B”解决方案:只需将名称打印到 data-* HTML5 属性中(例如 data-catname),并在 CSS 中使用 [data-*="XYZ"] 选择器,而不是普通的类. (这在我看来是一种 hack,但特殊字符与字符串无关。)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-07
    • 2021-04-07
    • 1970-01-01
    • 2015-11-07
    • 1970-01-01
    • 1970-01-01
    • 2022-11-13
    • 1970-01-01
    相关资源
    最近更新 更多