Wordpress and Php question: Categories into 5 columns?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tonyparra
    Confirmed User
    • Jul 2008
    • 4568

    #1

    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
  • zerovic
    Confirmed User
    • Apr 2010
    • 1116

    #2
    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
    Last edited by zerovic; 04-12-2012, 01:11 AM.
    php, html, jquery, javascript, wordpress - contact me at contact at zerovic.com

    Comment

    • Brujah
      Beer Money Baron
      • Jan 2001
      • 22157

      #3
      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);

      Comment

      • fris
        Too lazy to set a custom title
        • Aug 2002
        • 55679

        #4
        i usally only needed to split into 2, curious why 5?
        Since 1999: 69 Adult Industry awards for Best Hosting Company and professional excellence.

        Comment

        • tonyparra
          Confirmed User
          • Jul 2008
          • 4568

          #5
          Originally posted by fris
          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

          Comment

          Working...