Created
September 30, 2018 12:52
-
-
Save plugin-republic/1684e0afa08fa16f1f7f85383654d3b7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function st_popular_posts_shortcode( $atts, $content ) { | |
$pop_posts = get_transient( 'st_popular_posts' ); | |
if( false === $pop_posts ) { | |
$args = apply_filters( 'showcase_filter_popular_posts', array( | |
'orderby' => 'comment_count', | |
'posts_per_page' => 4, | |
) ); | |
$pop_posts = new WP_Query( $args ); | |
set_transient( 'st_popular_posts', $pop_posts, WEEK_IN_SECONDS ); | |
} | |
$current_post_id = get_the_ID(); | |
ob_start(); ?> | |
<div class="showcase-popular-posts"> | |
<div class="row"> | |
<?php if( $pop_posts->have_posts() ) { | |
$count = 1; | |
while ( $pop_posts->have_posts() ) : $pop_posts->the_post(); | |
global $post; | |
if( $count <= 3 && get_the_ID() != $current_post_id ) { | |
$class = array( 'blog-article', 'featured-article', 'col', 'col-xsmall-full', 'article-' . $count ); | |
$class[] = 'col-large-one-third';?> | |
<article id="post-<?php the_ID(); ?>" <?php post_class( $class ); ?>> | |
<div class="article-inner-wrapper"> | |
<div class="featured-image"> | |
<a href="<?php echo esc_url( get_permalink() ); ?>" rel="bookmark"> | |
<?php the_post_thumbnail(); ?> | |
</a> | |
</div> | |
<div class="entry-wrapper"> | |
<header class="entry-header"> | |
<?php | |
the_title( '<h4 class="entry-title"><a href="' . esc_url( get_permalink() ) . '" rel="bookmark">', '</a></h4>' ); ?> | |
</header><!-- .entry-header --> | |
<footer class="entry-footer"> | |
<a class="button secondary small" href="<?php echo esc_url( get_permalink() ); ?>" rel="bookmark"><?php _e( 'Read more', 'showcase' ); ?></a> | |
</footer><!-- .entry-footer --> | |
</div><!-- .entry-wrapper --> | |
</div><!-- .article-inner-wrapper --> | |
</article><!-- #post-## --> | |
<?php $count++; | |
} | |
endwhile; | |
} ?> | |
</div><!-- .row --> | |
</div><!-- .showcase-popular-posts --> | |
<?php $return = ob_get_clean(); | |
wp_reset_query(); | |
return $return; | |
} | |
add_shortcode( 'popular_posts', 'st_popular_posts_shortcode' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment