• I can’t seem to get the child_of parameter for the categories working

    What I have is
    Articles (ID = 4)
    – Accomodation (ID =7)
    – Food (ID = 6)
    – Money (ID = 5)

    I want to bring back all child categories of ID 4.

    $cats=array(
    'child_of'                 => 4
    );
    $categories = get_categories($cats);
    
    if ($categories) {
        $numcategories = count($categories);
    	$term = $categories[$i];
    	echo '
    		<div class="articles-post">
    		  <div class="articles-posttitlebox"><a href="' . get_category_link( $term->term_id ) . '">' . $term->cat_name.'</a></div>
    <img src="/wp/wp-content/uploads/category-images-ii/'.$term->term_id.'.original.jpg">
    		</div>';
    	}
      }

    For some reason, this only brings back the category food.

    Has anyone got any ideas?

Viewing 2 replies - 1 through 2 (of 2 total)
  • i would expect a kind of foreach loop to show all child categories.

    you only seem to work with one term: $term = $categories[$i];
    where is the $i defined?
    was that planned to be a for loop?
    https://php.net/manual/en/control-structures.for.php

    from your (incomplete) code segment, it is difficult to see what you are trying to achieve.

    Thread Starter Evostance

    (@evostance)

    Sorry, I have basically got it too loop the child categories of a set parent. However I have 2 columns so need them to go in order say:
    Category 1 – Col 1, Category 2 – Col 2, Category 3 – Col 1 etc etc

    I have got this to work, however it doesnt seem to pick the category up correctly

    My full code is:

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    
    <?php
    $cats=array(
    'child_of'                 => 4
    	);
    $categories = get_categories($cats);
    
    ?>
    <div id="articles-left">
    <?php
      if ($categories) {
        $numcategories = count($categories);
        for($i = 1; $i < $numcategories; $i = $i + 2) {
    	$term = $categories[$i];
    	echo '
    		<div class="articles-post">
    		  <div class="articles-posttitlebox"><a href="' . get_category_link( $term->term_id ) . '">' . $term->cat_name.'</a></div>
    <img src="/wp/wp-content/uploads/category-images-ii/'.$term->term_id.'.original.jpg">
    		</div>';
    	}
      }
    ?>
    
    </div>
    
    <div id="articles-right">
    <?php
      if ($categories) {
        $numcategories = count($categories);
        for($i = 2; $i < $numcategories; $i = $i + 2) {
    	$term = $categories[$i];
    	echo '
    		<div class="articles-post">
    		  <div class="articles-posttitlebox"><a href="' . get_category_link( $term->term_id ) . '">' . $term->cat_name.'</a></div>
    <img src="/wp/wp-content/uploads/category-images-ii/'.$term->term_id.'.original.jpg">
    		</div>';
    		}
      }
    ?>
    
    </div>
    <?php endwhile; else: ?>
    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
    <?php endif; ?>

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Category Child Of not functioning’ is closed to new replies.