WordPress-显示子对象的数组数据

时间:2011-05-17 作者:Rob

我使用了wordpress插件-advanced custom field 要显示一个子数组,下面是我的代码:

我使用此代码显示来自特定页面ID子级的自定义字段数据:

<?php $args = array(
    \'depth\'        => 0,
    \'child_of\'     => 411 );
$pages = get_pages(array(\'child_of\'));

foreach($pages as $post)
{
setup_postdata($post);
$fields = get_fields();
if($fields->company_name != "") : ?>
   <h2><?php echo $fields->company_name; ?></h2>
<?php else : ?>
<?php endif; ?> 
<?php
}

wp_reset_query();
?>
这是我用来显示页面本身的复选框数据(即它不是父/子)的代码:

<?php $catNames = array( \'branding\',\'creative\',\'development\',\'exhibition\',\'packaging\',\'print\',\'seo\',\'social_media\',\'usability\',\'web\',\'advertising\',\'campaign\',\'content\',\'feasibility\',\'publishing\',\'research\',\'strategy\');

    foreach($catNames as $name){
        if(in_array($name, get_field(\'categories\') )){
            echo \'<a href="/tags/design/\'.$name.\'" title="\'.$name.\'">\'.ucwords($name).\'</a>\';       
        }
    }
?> 
我想将两者结合在一起,因此在第一个代码框中的H2标记下面,我想显示子项的复选框数据,如何做到这一点?

更新:当我将复选框代码添加到子代码时,我无法获得要显示的数据,它要么为空,要么有错误。

这是我的页面working on. 第一个缩略图已经硬编码,第二个和第三个缩略图应该用上面的代码拾取标签,但它没有。

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

这将显示已勾选的复选框(更简单!!):

                    <?php
$categories = get_field(\'categories\');
$elements = array();
foreach($categories as $category) {
    //do something
    $elements[] = \'<a href="/tags/design/\' . strtolower($category) . \'" title="\' . $category . \'">\' . $category .\'</a>\';
}
echo implode(\',\', $elements);
?>

SO网友:Bainternet

Try this code:

<?php $args = array(
    \'depth\'        => 0,
    \'child_of\'     => 411 );
$pages = get_pages(array(\'child_of\'));
$catNames = array( \'branding\',\'creative\',\'development\',\'exhibition\',\'packaging\',\'print\',\'seo\',\'social_media\',\'usability\',\'web\',\'advertising\',\'campaign\',\'content\',\'feasibility\',\'publishing\',\'research\',\'strategy\');
foreach($pages as $post){
    setup_postdata($post);
    $company = get_field(\'company_name\',$post->ID);
    if( $company != "") {
       echo \'<h2>\'.$company.\'</h2>\';
       foreach($catNames as $key => $val){
            if(in_array($val, get_field(\'categories\',$post->ID) )){
                echo \'<a href="/tags/design/\'.$val.\'" title="\'.$val.\'">\'.ucwords($val).\'</a>\';       
            }
        }
    }
}
wp_reset_query();
?>
结束