自定义WordPress功能在Loop中不起作用


Custom WordPress function is not working inside Loop

我正在开发这个网站: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)并使页面不完整。

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

谢谢!

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

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>

但是你可能有自己的理由把它分开,所以只做你觉得最好的事情。