Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Breaks an ACF field into two divs of evenish columns
<?php
/**
*
* Breaks an ACF field into two divs of evenish columns
*
* @author Aaron John Schlosser <aaron@aaronschlosser.com>
* @link https://gist.github.com/ajschlosser/daf17648f0f42d1597ec#file-acf_make_two_columns
* @copyright Copyright 2014 Aaron John Schlosser
* @license http://www.gnu.org/licenses/gpl-2.0.html GPLv2
* @param string $field The ACF field name (e.g., "about_content")
* @param array $params Array containing all other parameters (optional)
* @param string $class The desired class to be added to each of the column divs (optional)
* @param string $delimiter The delimiter used to break up ACF field content into an iterable array (optional)
* @param string $tag Which HTML tag to surround the new paragraphs in (optional)
* @return null
*/
function acf_make_two_columns ($field, $params=null, $class="large-6 columns", $delimiter="<br />", $tag="p") {
if ($params && count($params) > 0) {
if ($params["class"]) $class = $params["class"];
if ($params["delimiter"]) $delimiter = $params["delimiter"];
if ($params["tag"]) $tag = $params["tag"];
}
$a = explode($delimiter, get_field($field));
$l = count($a);
$n = strpos($tag, " ");
$open_tag = $tag;
if ($n === false) {
$n = strlen($tag);
$close_tag = "</".substr($tag,1,$n-1);
} else $close_tag = "</".substr($tag,1,$n-1).">";
$total_length = 0;
for($i = 1; $i <= $l; $i++) {
$total_length += strlen($a[$i]);
}
$first_half_length = 0;
$second_half_length = 0;
$second_half_index = 0;
$done = false;
echo "<!--Begin Automatically Generated Columns. Attempting to divide ".$l." paragraphs into two columns each with a class of 'columnize ".$class."'.--><div class='columnize ".$class."'>".$open_tag.$a[0].$close_tag;
for($i = 1; $i <= $l; $i++) {
if (!$done) {
$first_half_length += strlen($a[$i]);
if ($first_half_length < ceil($total_length/2)) {
echo $open_tag.$a[$i].$close_tag;
}
else {
echo $open_tag.$a[$i].$close_tag;
$done = true;
$second_half_index = $i;
$second_half_length = $total_length - $first_half_length;
}
}
}
echo "</div><div class='columnize large-6 columns'>";
if ($second_half_index > 0) {
for($i = $second_half_index; $i <= $l; $i++) {
echo $open_tag.$a[$i].$close_tag;
}
} else {
echo "There was a problem splitting the ACF field into two even columns. Sorry!";
}
echo "</div><!--End Automatically Generated Columns.-->";
return null;
}
?>
@ajschlosser

This comment has been minimized.

Copy link
Owner Author

ajschlosser commented Sep 5, 2014

Add this to the functions.php file of your Wordpress installation using the Advanced Custom Fields plugin. Requires the Advanced Custom Fields plugin to work.

Possible implementation given an ACF field with a name of "about_content":

<?php
    $params = array("class"=>"col-md-6", "delimiter"=>"</p>", "tag"=>"<div class='myDiv'>");
    acf_make_two_columns("about_content", $params);
?>

The $params array is not necessary if you want to use the default settings. Or, you can pass all the parameters as strings after passing the $params parameter as null:

<?php
    acf_make_two_columns("about_content", null, "col-md-6", "</p>", "<div class='myDiv'>");
?>

Have fun.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.