多个分类选择仅在前端显示一个

时间:2015-08-03 作者:tibewww

我有各种自定义分类,

有时,我会在后端选择多个选项(cf屏幕截图)

enter image description here

然而,在前端,我只有一个得到显示,我想他们都被显示在前端。。

这是我调用它们在前端显示的方式:

  <?php
$taxonomy = wp_get_post_terms( $post->ID, \'category_job\');
$locations = wp_get_post_terms( $post->ID, \'locations\', array("fields" => "all"));
$types = wp_get_post_terms( $post->ID, \'types\');
$sectors = wp_get_post_terms( $post->ID, \'sectors\');
$salaries = wp_get_post_terms( $post->ID, \'salaries\');
$price = wp_get_post_terms( $post->ID, \'price-range\');
echo \'<strong>Category:</strong> \'.$taxonomy[0]->name.\'<br>\';
echo \'<strong>Location:</strong> \'.$locations[0]->name.\'<br>\';
echo \'<strong>Sector:</strong> \'.$sectors[0]->name.\'<br>\';
echo \'<strong>Salary:</strong> \'.$price[0]->name.\'£\'.\'<br>\';
echo \'<strong>Type:</strong> \'.$types[0]->name.\'<br>\';

?>
任何帮助都将是惊人的:)

谢谢你们的时间伙计们!

1 个回复
最合适的回答,由SO网友:mmm 整理而成

添加循环以显示所有分类:

if (2 > count($locations)) {
    echo \'<strong>Location:</strong> \'.$locations[0]->name.\'<br>\';
} else {

    echo "<strong>Location:</strong>";

    echo "<ul>";

    foreach ($locations as $l) {
        echo "<li>";
        echo $l->name;
        echo "</li>";
    }

    echo "</ul>";

}

所有位置在同一行的其他循环

    $stack = array();

    foreach ($locations as $l) {
        $stack[] = $l->name;
    }

    echo "<strong>Location:</strong>" . implode(";", $stack) . "<br/>";

结束