0

我正在开发这个网站:baikumotor.com

我在里面插入了以下定制函数functions.php

function colorStock(){
    function display_color($color)
    {
        echo "<div class=\"colorStockItem\" style=\"background: ". $color .";\"></div>";
    }   

    $postID = get_the_ID();
    $colorStock = get_post_meta($postID, 'colorStock', true); //Get Colours available

    if ($colorStock != ""){
        $myArray = explode(', ', $colorStock);
            //print_r($myArray); 
            //echo $myArray;

        foreach ($myArray as $item){ 

            $css_colors = array('naranja' => '#f58e08', 
                                    'rojo' => '#d40000',
                                    'azul' => '#3086d6',
                                    'blanco' => '#ffffff',
                                    'negro' => '#000000',
                                    'plata' => '#d0d0d0');

            foreach ( $css_colors as $colorname => $value) {
                if ($colorname == $item) {
                    display_color($value);
                } 
            }
        } 
    }


}

它应该显示每辆自行车可用的颜色。通过从每个帖子的自定义 meta_field 中获取颜色的名称(如果可用)。

问题是,每当我将colorStock();函数放在产品循环中时,它都会在最新的自行车中正常加载,但是一旦它到达应该从下一个产品加载颜色的地步,它就会停止加载内容( HTML) 并使页面不完整。

我想知道为什么它会破坏页面加载以及如何解决这个问题。

谢谢!

4

1 回答 1

1

我会以不同的方式组织一切。尝试像这样重写你的函数:

function colorStock($colors)
{
    $css_colors = array('naranja' => '#f58e08', 
                        'rojo' => '#d40000',
                        'azul' => '#3086d6',
                        'blanco' => '#ffffff',
                        'negro' => '#000000',
                        'plata' => '#d0d0d0');

    $colors = str_replace(' ', '', $colors); //Strip out the spaces first
    $myArray = explode(',', $colors);
    $output = '';

    foreach($css_colors as $colorname => $value){
        if(in_array($colorname, $myArray)
            $output .= '<div class="colorStockItem" style="background: '.$value.';"></div>';
    }
    return $output;
}

然后像这样调用你的循环:

<?php
$postID = get_the_ID();
$colorstock = get_post_meta($postID, 'colorStock', true);
if(have_posts()) : while(have_posts()) : the_post();
?>
<!-- YOUR LOOP CONTENTS -->
<?php
//YOUR COLORS
echo $colorstock ? colorStock($colorstock) : 'No colors found';

endwhile;endif;
?>

附加建议:为了使事情更有条理,您可能可以完全取消检查颜色数组,并坚持使用样式表中的类名来定义背景颜色:

function colorStock($colors)
{
    $colors = str_replace(' ', '', $colors); //Strip out the spaces first
    $myArray = explode(',', $colors);
    $output = '';

    foreach($myArray as $class)
        $output .= '<div class="'.$class.' colorStockItem"></div>';

    return $output;
}

像这样调用:

<?php
$postID = get_the_ID();
$colorstock = get_post_meta($postID, 'colorStock', true);
if(have_posts()) : while(have_posts()) : the_post();
?>
<!-- YOUR LOOP CONTENTS -->
<?php
//YOUR COLORS
echo $colorstock ? colorStock($colorstock) : 'No colors found';

endwhile;endif;
?>

然后在您的样式表中:

<style type="text/css">
.colorStockItem{background: #000;} /*DEFAULT*/
.naranja{background: #f58e08;}
.rojo{background: #d40000;}
.azul{background: #3086d6;}
.blanco{background: #FFF;}
.negro{background: #000;}
.plata{background: #d0d0d0;}
</style>

但是你可能有你的理由把它分开,所以只做你认为最好的事情。

于 2012-05-25T01:40:25.723 回答