![]() |
![]() |
![]() |
||||
Welcome to the GoFuckYourself.com - Adult Webmaster Forum forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact us. |
![]() ![]() |
|
Discuss what's fucking going on, and which programs are best and worst. One-time "program" announcements from "established" webmasters are allowed. |
|
Thread Tools |
![]() |
#1 |
Confirmed User
Industry Role:
Join Date: Jul 2008
Location: In your back seat with duck tape
Posts: 4,568
|
Wordpress and Php question: Categories into 5 columns?
Ok Ive been busing this snippet to make 3 columns:
Code:
<?php $catArray = explode("</li>",wp_list_categories('title_li=&echo=0&depth=1')); $catCount = count($catArray) - 1; $catColumns = round($catCount / 3); $twoColumns = round($catColumns + $catColumns); for ($i=0;$i<$catCount;$i++) { if ($i<$catColumns){ $catLeft = $catLeft.''.$catArray[$i].'</li>'; } elseif ($i<$twoColumns) { $catMiddle = $catMiddle.''.$catArray[$i].'</li>'; } elseif ($i>=$catColumns){ $catRight = $catRight.''.$catArray[$i].'</li>'; } }; ?> <ul class="left"> <?php echo $catLeft; ?> </ul> <ul class="middle"> <?php echo $catMiddle; ?> </ul> <ul class="right"> <?php echo $catRight; ?> </ul> Code:
<?php $catArray = explode("</li>",wp_list_categories('title_li=&echo=0&depth=1')); $catCount = count($catArray) - 1; $catColumns = round($catCount / 5); $twoColumns = round($catColumns * 2); for ($i=0;$i<$catCount;$i++) { if ($i<$catColumns){ $catLeft = $catLeft.''.$catArray[$i].'</li>'; } elseif ($i<$twoColumns) { $catMiddle = $catMiddle.''.$catArray[$i].'</li>'; } elseif ($i>=$catColumns){ $catRight = $catRight.''.$catArray[$i].'</li>'; } elseif ($i>=$catColumns){ $catRight = $catRight2.''.$catArray[$i].'</li>'; } elseif ($i>=$catColumns){ $catRight = $catRight3.''.$catArray[$i].'</li>'; } }; ?> <ul class="left"> <?php echo $catLeft; ?> </ul> <ul class="middle"> <?php echo $catMiddle; ?> </ul> <ul class="right"> <?php echo $catRight; ?> </ul> <ul class="right"> <?php echo $catRight2; ?> </ul><ul class="right"> <?php echo $catRight3; ?> </ul>
__________________
High Performance Vps $10 Linode Manage your Digital Ocean, Linode, or Favorite Cloud Server. Simple, fast, and secure Server Pilot |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#2 |
Confirmed User
Industry Role:
Join Date: Apr 2010
Posts: 1,084
|
I've re-wrote the code for you, to make it work with 5 columns...
please note, if the total number is like 23, which can't be divided by 5, it will add more categories to the last column Code:
$catArray = explode("</li>",wp_list_categories('title_li=&echo=0&depth=1')); $catCount = count($catArray) - 1; $catColumns = round($catCount / 5); $catColumns_2 = $catColumns * 2; $catColumns_3 = $catColumns * 3; $catColumns_4 = $catColumns * 4; $catColumns_5 = $catColumns * 5; $i = 0; foreach($catArray as $categories) { if($i < $catColumns) { $first_column .= $categories . "</li>"; $i++; } elseif($i < $catColumns_2) { $second_column .= $categories . "</li>"; $i++; } elseif($i < $catColumns_3) { $third_column .= $categories . "</li>"; $i++; } elseif($i < $catColumns_4) { $fourth_column .= $categories . "</li>"; $i++; } else { $fifth_column .= $categories . "</li>"; $i++; } } ?> <ul class="first"> <?php echo $first_column; ?> </ul> <ul class="second"> <?php echo $second_column; ?> </ul> <ul class="third"> <?php echo $third_column; ?> </ul> <ul class="fourth"> <?php echo $fourth_column; ?> </ul> <ul class="fifth"> <?php echo $fifth_column; ?> </ul>
__________________
php, html, jquery, javascript, wordpress - contact me at contact at zerovic.com |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#3 |
Beer Money Baron
Industry Role:
Join Date: Jan 2001
Location: brujah / gmail
Posts: 22,157
|
This should display your categories, in column lists of any specified amount. I've told it to display 3 columns at bottom. If you want it to also link to them, let me know.
Code:
function columnize_categories ( $columns = 5 ) { $wp_categories = get_categories( array( 'hide_empty' => 0 ) ); $categories = array(); $per = ceil( count( $wp_categories ) / $columns ); $i = 0; while ( $cats = array_splice( $wp_categories, 0, $per ) ) { $categories[$i++] = $cats; } $output = ''; foreach ( $categories as $list ) { $output .= '<ul>'; foreach ( $list as $item ) { $output .= sprintf( '<li>%s</li>', $item->name ); } $output .= '</ul>'; } return $output; } echo columnize_categories(3);
__________________
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#5 |
Confirmed User
Industry Role:
Join Date: Jul 2008
Location: In your back seat with duck tape
Posts: 4,568
|
For a site im doing i need the columns to be split into 5 to fit across the main content wrap area. If i make sense at all lol.
__________________
High Performance Vps $10 Linode Manage your Digital Ocean, Linode, or Favorite Cloud Server. Simple, fast, and secure Server Pilot |
![]() |
![]() ![]() ![]() ![]() ![]() |