将复选框数据与页面标题匹配以显示某些项目

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

我想显示已将其复选框勾选为品牌的项目,如果它位于品牌页面上(即页面标题为品牌)。

要稍微解释一下代码:

这一行显示了每个项目勾选的所有复选框,因此如果勾选了,它将输出“品牌”、“网络”、“打印”。

implode(\', \',get_field(\'categories\')
下一行只是检查页面标题是否为“品牌”:

implode(\', \',get_field(\'categories\')
我试图将它们都放在一个if语句中,它只输出复选框,如果它们与标题匹配,则输出它们。

<?php if(implode(\', \',get_field(\'categories\')) && $grid_title == "Branding"); {
echo "testing"; 
}
?>
上面的代码显示了我想要做的事情,但它不太管用。

IMPORTANT: I\'m using this plugin to create the custom checkboxes so please bear that in mind.

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

我自己通过阅读找到了答案,解决了这个问题:

   <div id="grid">
<?php $args = array(
    \'depth\'        => 0,
    \'child_of\'     => 411 );
$pages = get_pages(array(\'child_of\'));
$grid_title = $post->post_title;

foreach($pages as $post)
{
setup_postdata($post);
$fields = get_fields();?>

<?php if($fields->company_name != "") : ?>
<!-- Any new tags will need to be added below -->
<?php if((in_array("Branding", get_field(\'categories\')) && $grid_title == "Branding") || (in_array("Creative", get_field(\'categories\')) && $grid_title == "Creative") || (in_array("Development", get_field(\'categories\')) && $grid_title == "Development") || (in_array("Exhibition", get_field(\'categories\')) && $grid_title == "Exhibition") || (in_array("Packaging", get_field(\'categories\')) && $grid_title == "Packaging") || (in_array("Print", get_field(\'categories\')) && $grid_title == "Print") || (in_array("SEO", get_field(\'categories\')) && $grid_title == "SEO") || (in_array("Usability", get_field(\'categories\')) && $grid_title == "Usability") || (in_array("Web", get_field(\'categories\')) && $grid_title == "Web") || (in_array("Campaign", get_field(\'categories\')) && $grid_title == "Campaign") || (in_array("Copywriting", get_field(\'categories\')) && $grid_title == "Copywriting") || (in_array("Feasibility", get_field(\'categories\')) && $grid_title == "Feasibility") || (in_array("Research", get_field(\'categories\')) && $grid_title == "Research") || (in_array("Social Media", get_field(\'categories\')) && $grid_title == "Social Media")){
     echo "
        <div class=\\"grid-box\\" onclick=\\"location.href=\'" . get_page_link($post->ID) ."\';\\" style=\\"cursor: pointer;\\">
        <div class=\\"phase-1\\">
           <img class=\\"grid-image\\" src=\\"" . $fields->thumb_image . "\\" alt=\\"" . $fields->company_name ."\\" height=\\"152\\" width=\\"210\\" />
           <div class=\\"grid-heading\\">
                <h2>". $fields->company_name ."</h2>
                <h3>" . implode(\', \',get_field(\'categories\')) ."</h3>
            </div> 
        </div>
        <div class=\\"phase-2\\">
            <div class=\\"grid-info\\">
                <h4>". $fields->project_name ."</h4>
                <p>". $fields->description ."</p>
            </div>
            <div class=\\"grid-heading-hover\\">
                <h2>". $fields->company_name ."</h2>
                <h3>". implode(\', \',get_field(\'categories\')) ."</h3>
            </div> 
        </div>
    </div>
     ";
}
else {
    echo "  <div class=\\"grid-box\\" onclick=\\"location.href=\'" . get_page_link($post->ID) ."\';\\" style=\\"cursor: pointer;\\">
        <div class=\\"phase-1\\">
           <img class=\\"grid-image\\" src=\\"" . $fields->thumb_image . "\\" alt=\\"" . $fields->company_name ."\\" height=\\"152\\" width=\\"210\\" />
           <div class=\\"grid-heading\\">
                <h2>". $fields->company_name ."</h2>
                <h3>" . implode(\', \',get_field(\'categories\')) ."</h3>
            </div> 
        </div>
        <div class=\\"phase-2\\">
            <div class=\\"grid-info\\">
                <h4>". $fields->project_name ."</h4>
                <p>". $fields->description ."</p>
            </div>
            <div class=\\"grid-heading-hover\\">
                <h2>". $fields->company_name ."</h2>
                <h3>". implode(\', \',get_field(\'categories\')) ."</h3>
            </div> 
        </div>
    </div>";
}

else : ?>
<?php endif; ?>    

<?php
}

wp_reset_query();
?>        
</div>

结束