0

I have a code which shows categories list with images. Im using this plugin to attach image to category.

Code is:

<?php
$args=array(
  'orderby' => 'name',
  'order' => 'ASC',
)
?>
 <?php foreach (get_categories( $args ) as $cat) : ?>
<h3><a href="<?php echo get_category_link($cat->term_id); ?>"><?php echo $cat->cat_name; ?></a></h3>
<a href="<?php echo get_category_link($cat->term_id); ?>"><img src="<?php echo z_taxonomy_image_url($cat->term_id); ?>" /></a>
 <?php endforeach; ?>

My question is how can I modify this loop to add something (separator or text or something) every 3 categories. So theres 3 categories then separator then 3 categories etc.

closed as off-topic by bueltge, TheDeadMedic, Pat J, cybmeta, Howdy_McGee Dec 28 '16 at 22:46

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    This is a basic php issue. Add a counter to your loop. – cjbj Nov 16 '16 at 20:25
3
<?php
$args=array(
  'orderby' => 'name',
  'order' => 'ASC',
)

$count=1; // A $count variable
?>
<?php foreach (get_categories( $args ) as $cat) :
if($count%3==0) // This condition will be true for 3,6,9,12..........
{
    //Your Code --- 
}
$count++ // Increment $count variable
 ?>
<h3><a href="<?php echo get_category_link($cat->term_id); ?>"><?php echo $cat->cat_name; ?></a></h3>
<a href="<?php echo get_category_link($cat->term_id); ?>"><img src="<?php echo z_taxonomy_image_url($cat->term_id); ?>" /></a>
<?php endforeach; ?>

I have added comments to understand.

  • Ranuka your code works great but I encountered a problem. I have an archive page which shows categories with images. Theres 12 categories per year (1 per each month). Every new month has new category like "september 2016" and each category has its own image. I wanted to use your code to make a separator every 12 categories to mark a year but I realised now that on the page Im building one of early years has 11 categories not 12 and everything screws up. How can I make this code detect post year change instead of counting to 12? I know that taxonomies dont store date... :/ – th3rion Nov 19 '16 at 20:07
  • @th3rion I wrote a answer for your question here (wordpress.stackexchange.com/questions/246750/…) – Ranuka Nov 20 '16 at 5:13

Not the answer you're looking for? Browse other questions tagged or ask your own question.