Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save davidtrushkov/fc540973dfcda069f3502a93908ef17d to your computer and use it in GitHub Desktop.
Save davidtrushkov/fc540973dfcda069f3502a93908ef17d to your computer and use it in GitHub Desktop.
This is how you would switch the image container position in a PHP foreach loop using WordPress.
<div class="container-fluid no-padding hidden-xs">
<?php foreach (get_posts(['post_type' => 'home_project_section', 'posts_per_page'=>100]) as $key => $homeProjects) {
$thumb_id = get_post_thumbnail_id($homeProjects);
$thumb_url_array = wp_get_attachment_image_src($thumb_id, 'thumbnail-size', true);
$thumb_url = $thumb_url_array[0];
$website_url = '/projects/';
$url_to_project = $website_url . $homeProjects->post_name;
?>
<?php if($key % 2 == 0) { ?>
<div class="col-xs-12 no-padding">
<div class="col-sm-7 col-md-6 home-projects-project-container">
<h1 class="home-projects-header">
<?= $homeProjects->post_title ?>
</h1>
<p class="home-projects-description">
<?= $homeProjects->post_content ?>
</p>
<br />
<a href="<?= $url_to_project; ?>" class="btn btn-primary btn-light-blue">
View Case Study
</a>
</div>
<div class="col-sm-5 col-md-6 no-padding">
<div style="background: url('<?= $thumb_url; ?>') center center; background-size: cover;" class="home-projects-image"></div>
</div>
</div>
<?php } else { ?>
<div class="col-xs-12 no-padding">
<div class="col-sm-5 col-md-6 no-padding">
<div style="background: url('<?= $thumb_url; ?>') center center; background-size: cover;" class="home-projects-image"></div>
</div>
<div class="col-sm-7 col-md-6 home-projects-project-container">
<h1 class="home-projects-header">
<?= $homeProjects->post_title ?>
</h1>
<p class="home-projects-description">
<?= $homeProjects->post_content ?>
</p>
<br />
<a href="<?= $url_to_project; ?>" class="btn btn-primary btn-light-blue">
View Case Study
</a>
</div>
</div>
<?php } ?>
<?php } ?>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment