Home › Forums › Front-end Issues › HowTo: build tables with acf and merged cells (colspan) › Reply To: HowTo: build tables with acf and merged cells (colspan)
updated (better) version with prepend and append elements and support for regular text input fields and without extra function:
function make_cells($field){
global $post, $posts;
$i = 1; // variable counter, may reset during foreach
$b = 1; // global counter, never resets
$end = count($posts);
echo "<td";
foreach($posts as $key => $post) {
$value = get_field($field);
$valueobj = get_field_object($field);
$nextvalue = "";
if(isset($posts[$key+1]))
$nextvalue = get_field($field,$posts[$key+1]->ID);
if($nextvalue != $value){
echo " colspan='".$i."'>";
if(isset($valueobj["prepend"])) echo $valueobj["prepend"]." ";
if(isset($valueobj["choices"])){
if(is_array($value)){
$newarray = [];
foreach($value as $val){
$newarray[] = $valueobj["choices"][$val];
}
echo implode(", ",$newarray);
}else{
echo $valueobj["choices"][$value];
}
}else{
echo $value;
}
if(isset($valueobj["append"])) echo " ".$valueobj["append"];
$i=0;
if($b != $end) echo "</td><td";
}
if($b == $end) echo "</td>";
$i++;
$b++;
}
}
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.