Skip to content

Instantly share code, notes, and snippets.

@RobDWaller
Created February 12, 2018 18:08
Show Gist options
  • Save RobDWaller/1cfb5aaed9b1403836530d03a05249f1 to your computer and use it in GitHub Desktop.
Save RobDWaller/1cfb5aaed9b1403836530d03a05249f1 to your computer and use it in GitHub Desktop.
An example of some awful PHP code I once reviewed. Saved for posterity.
<?php
function blogPreviews($numberOfPosts)
{
$blogPreviews = [];
$args = array(
'numberposts' => $numberOfPosts
);
$blogPostsData = get_posts($args);
$themeColors = array(
'1' => 'coral',
'2' => 'blue',
'3' => 'turquoise',
'4' => 'blue',
'5' => 'purple',
'6' => 'maroon',
'7' => 'green'
);
$styles = array(
'1' => 'wide',
'2' => 'full',
'3' => 'short',
'4' => 'full',
'5' => 'short',
'6' => 'full',
'7' => 'wide'
);
if ($blogPostsData) {
$count = 0;
foreach ($blogPostsData as $currentPost) {
$count++;
if ($count == '1' || $count == '7') {
$class = "medium-first";
} else if ($count == '2' || $count == '5') {
$class = "small-last";
} else {
$class = "small";
}
$imageAttr = array(
'class' => 'c-blog-block__image--' . $styles[$count]
);
$blogPreview = array(
'title' => $currentPost->post_title,
'theme_color' => $themeColors[$count],
'link' => get_permalink($currentPost->ID),
'image' => get_the_post_thumbnail($currentPost->ID,array('300','200'), $imageAttr),
'style' => $styles[$count],
'class' => $class,
'text' => get_the_excerpt($currentPost->ID),
'id' => $currentPost->ID
);
$blogPreviews[] = $blogPreview;
}
}
return $blogPreviews;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment