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.

Post New Thread Reply

Register GFY Rules Calendar
Go Back   GoFuckYourself.com - Adult Webmaster Forum > >
Discuss what's fucking going on, and which programs are best and worst. One-time "program" announcements from "established" webmasters are allowed.

 
Thread Tools
Old 04-11-2012, 11:22 PM   #1
tonyparra
Confirmed User
 
tonyparra's Avatar
 
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>
It works great however I keep screwing up in adding extra columns
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>
I know that is fucked anybody know how to do this or expand on the 3 column code correctly?
__________________

High Performance Vps $10 Linode
Manage your Digital Ocean, Linode, or Favorite Cloud Server. Simple, fast, and secure Server Pilot
tonyparra is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-12-2012, 02:07 AM   #2
zerovic
Confirmed User
 
zerovic's Avatar
 
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>
hope it's ok :P
__________________
php, html, jquery, javascript, wordpress - contact me at contact at zerovic.com

Last edited by zerovic; 04-12-2012 at 02:11 AM..
zerovic is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-12-2012, 03:55 AM   #3
Brujah
Beer Money Baron
 
Brujah's Avatar
 
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);
__________________
Brujah is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-12-2012, 05:51 AM   #4
fris
Too lazy to set a custom title
 
fris's Avatar
 
Industry Role:
Join Date: Aug 2002
Posts: 55,235
i usally only needed to split into 2, curious why 5?
__________________
Since 1999: 69 Adult Industry awards for Best Hosting Company and professional excellence.


WP Stuff
fris is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-12-2012, 06:34 AM   #5
tonyparra
Confirmed User
 
tonyparra's Avatar
 
Industry Role:
Join Date: Jul 2008
Location: In your back seat with duck tape
Posts: 4,568
Quote:
Originally Posted by fris View Post
i usally only needed to split into 2, curious why 5?
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
tonyparra is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Post New Thread Reply
Go Back   GoFuckYourself.com - Adult Webmaster Forum > >

Bookmarks



Advertising inquiries - marketing at gfy dot com

Contact Admin - Advertise - GFY Rules - Top

©2000-, AI Media Network Inc



Powered by vBulletin
Copyright © 2000- Jelsoft Enterprises Limited.