• Resolved Atticus_Finch12

    (@atticus_finch12)


    I have attempted to use some code to pull all of the children of a specific category, and then display in three columns. However my attempts have been unsuccessful thus far. Below is my code, and any help would be greatly appreciated:

    ?php
    $catArray = explode(“”,wp_list_categories(‘orderby=name&show_count=1&use_desc_for_title=0&child_of=8&exclude=&title_li=’));
    $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].”;
    }
    elseif ($i<$twoColumns) {
    $catMiddle = $catMiddle.”.$catArray[$i].”;
    }
    elseif ($i>=$catColumns){
    $catRight = $catRight.”.$catArray[$i].”;
    }
    };
    ?>

Viewing 4 replies - 1 through 4 (of 4 total)
  • [please use the code button or backticks to mark code]

    a few things:

    – if you want ‘wp_list_categories()’ to generate a string to explode, you need to add ‘&echo=0’ to the args;

    – ‘&exclude=’ is empty and could be removed;

    – you haven’t defined a character for the ‘explode’; it is an empty string at the moment; '</li>' might be suitable;

    – the same character is needed to ‘cement’ the bits; for instance in these parts $catArray[$i].''; instead of the empty string ''

    – there is no output of the resulting strings; something like echo $catLeft; echo $catMiddle; echo $catRight; at the end might do;

    https://codex.www.ads-software.com/Template_Tags/wp_list_categories

    Thread Starter Atticus_Finch12

    (@atticus_finch12)

    Sorry for not using the code button. Thanks for the advice of adding the &echo=0 that was the part I seemed to be missing. For anyone else interested here is the code below:

    <?php
    	$catArray = explode("",wp_list_categories('orderby=name &echo=0&show_count=1&use_desc_for_title=0&child_of=8&exclude=&title_li='));
    	$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].'';
    	         }
    		elseif ($i<$twoColumns) {
    			$catMiddle = $catMiddle.''.$catArray[$i].'';
    		}
    		elseif ($i>=$catColumns){
    			$catRight = $catRight.''.$catArray[$i].'';
    		 }
    	};
    ?>
    
    <ul class="left">
          <?php echo $catLeft; ?>
    
    <ul class="middle">
    	<?php echo $catMiddle; ?>
    
    <ul class="right">
          <?php echo $catRight; ?>

    [mod: backticks are the character at the top left of the keyboard, to the left of number 1;
    using the ‘code’ button:
    to mark the code, click the code button once before entering the code, then once after;
    or highlight the code and press the code button once]

    don’t forget to close the <ul> tags in your output;

    i am still surprised that the explode works with an empty string delimiter (?)
    https://php.net/manual/en/function.explode.php

    Thread Starter Atticus_Finch12

    (@atticus_finch12)

    Thanks for the find on the ‘

      ‘ tag. I am suprised as well on the explode function especially after reading the documentation.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘multi column child category’ is closed to new replies.