Skip to content

Instantly share code, notes, and snippets.

@mrwweb
Created September 20, 2013 00:19
Show Gist options
  • Save mrwweb/6631679 to your computer and use it in GitHub Desktop.
Save mrwweb/6631679 to your computer and use it in GitHub Desktop.
Jetpack Buggy Featured Content Code

== Observed Behaviour ==

Output is correct and the feature generally works as expected. max_posts is set to 2 in both Settings > Reading and functions.php. However, the code above outputs as many posts as are given the "Home Feature" tag.

== Expected Behavior ==

The wcp_get_featured_posts() function would only return 2 posts. (probably the most recent 2?)

== Other Notes ==

This code is from a child theme of a horrible Elegant Themes theme—not by choice. You'll note that the output snippet appears in home.php which is used to output a static front page based on a proprietary setting that ignores the normal front page setting on Settings > Reading.

I wonder if this is somehow related to the home.php misuse. In general, I can make no sense of the "Templates" section of the Featured Content documentation but it seems like it might be relevant.

<?php
add_image_size( 'wcp-front-feature', 440, 300, true );
add_theme_support( 'featured-content', array(
'featured_content_filter' => 'wcp_featured_posts',
'max_posts' => 2,
) );
function wcp_get_featured_posts() {
return apply_filters( 'wcp_featured_posts', array() );
}
function wcp_has_featured_posts( $minimum = 0 ) {
if ( is_paged() )
return false;
$minimum = absint( $minimum );
$featured_posts = apply_filters( 'mytheme_get_featured_posts', array() );
if ( ! is_array( $featured_posts ) )
return false;
if ( $minimum > count( $featured_posts ) )
return false;
return true;
}
<?php
// this is the relevant portion of the template file
if( wcp_has_featured_posts() ) :
$featured_posts = wcp_get_featured_posts();
$post_count = count($featured_posts);
?>
<div class="wcp-featured-posts post-count-<?php echo $post_count; ?>">
<?php foreach( $featured_posts as $post ) :
setup_postdata( $post ); ?>
<div class="hentry wcp-featured-post">
<h2 class="entry-title title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<div class="thumb"><a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( 'wcp-front-feature'); ?><span class="overlay"></span></a></div>
<div class="entry-summary"><?php the_excerpt(); ?></div>
<a class="readmore" href="<?php the_permalink(); ?>"><span>Read More&hellip;</span></a>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment