Best way to alternate the custom posts on archive pages

Hi quick question, getting my head around Sage’s template structure. Understand that content-custom.php is for custom post types and archive-custom.php if for its archive page. My question what is best practice to alternate the layouts based on conditionals? Say every second custom post i want to swap the image to the right as apposed to left which is normal, would you keep the code below but attach extra text behind it like below or do you just scrape the template content approach.

<?php if ($counter % 2 == 0):?>
<?php get_template_part('templates/content', get_post_type() != 'post' ? get_post_type() : get_post_format(),'-2'); ?>
<?php else:?>
 <?php get_template_part('templates/content', get_post_type() != 'post' ? get_post_type() : get_post_format()); ?>
<?php endif;?>

I tried to do the above but i can’t seem to get it working. I think as it already grabbed the content template beforehand

This isn’t a Sage/Roots specific question and prefer to not have these sorts of questions asked here (you mentioned Sage’s template structure but that isn’t relevant to what you’re trying to do).

Rather than using PHP, this can easily be achieved with CSS with :even or :odd selectors.

1 Like