Skip to content

Instantly share code, notes, and snippets.

@benjaminpick
Created February 26, 2015 10:44
Show Gist options
  • Save benjaminpick/9a5e90f60018fb1273f8 to your computer and use it in GitHub Desktop.
Save benjaminpick/9a5e90f60018fb1273f8 to your computer and use it in GitHub Desktop.
This is a simple wordpress plugin to show the news on the home page as a shortcode.
<?php
/*
Plugin Name: Spring News Shortcode
Description: Provides a shortcode [spring_news col="4" nb="8"].
Author: Benjamin Pick
Version: 0.1
License: GPLv3 or later
License URI: http://www.gnu.org/licenses/gpl-3.0.html
*/
function spring_news_shortcode_code($attr) {
$cols = isset($attr['col']) ? (int) $attr['col'] : 4;
$nb = isset($attr['nb']) ? (int) $attr['nb'] : 8;
$news = get_posts(array(
'post_type' => 'post',
'posts_per_page' => $nb,
'orderby' => 'post_date',
));
$bootstrap_classes = "";
if (function_exists('get_boostrap_col'))
$bootstrap_classes = get_boostrap_col($cols);
$rows = array_chunk($news, $cols);
$html = '';
foreach ($rows as $row) :
$html .= '<div class="row spring_news">';
foreach ($row as $n) :
$link = '<a href="' . get_permalink($n->ID) . '">';
$datum = date_i18n(get_option('date_format'), strtotime($n->post_date));
$html .= '<div class="' . $bootstrap_classes . '">';
$html .= '<span class="datum">' . $datum . '</span>';
$html .= $link . get_the_post_thumbnail($n->ID, 'thumbnail', array('class' => 'responsive')) . '</a>';
$html .= '<h3>' . $link . $n->post_title . '</a></h3>';
$html .= '</div>';
endforeach;
$html .= '</div>';
endforeach;
return $html;
}
add_shortcode('spring_news', 'spring_news_shortcode_code');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment