Last active
August 29, 2015 14:23
-
-
Save mikeyarce/a0a662a2ccdc46784392 to your computer and use it in GitHub Desktop.
Jetpack's Featured Content
This file contains hidden or 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
// Add Theme support and write a function to get the featured posts | |
// This goes in your themes functions.php file | |
add_theme_support( 'featured-content', array( | |
'filter' => 'mytheme_get_featured_posts', | |
'max_posts' => 20, | |
'post_types' => array( 'post', 'page' ), | |
) ); | |
function mytheme_get_featured_posts() { | |
return apply_filters( 'mytheme_get_featured_posts', array() ); | |
} | |
function mytheme_has_featured_posts( $minimum = 1 ) { | |
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; | |
} |
This file contains hidden or 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
// Now lets call the featured posts using a template part | |
// In this scenario, you are only wanting them to show up on your front page | |
<?php | |
if ( is_front_page() && mytheme_get_featured_posts() ) { | |
// Include the featured content template. | |
get_template_part( 'content', 'featured' ); | |
} | |
?> |
This file contains hidden or 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 | |
/** | |
* The template for displaying featured content | |
*/ | |
?> | |
<?php | |
/** | |
* Let's show some Featured Content | |
*/ | |
if( is_front_page() && mytheme_get_featured_posts() ) : | |
$featured_posts = mytheme_get_featured_posts(); | |
$post_count = count($featured_posts); | |
?> | |
<div class="featured-posts"> | |
<?php foreach( $featured_posts as $post ) : | |
setup_postdata( $post ); ?> | |
<figure class="cap-left"> | |
<a href="<?php the_permalink(); ?>" title="<?php the_title();?>"> | |
<figcaption> | |
<?php the_title(); ?> | |
</figcaption> | |
</a> | |
</figure> | |
<?php endforeach; ?> | |
</div> <!-- .featured-posts !--> | |
<?php endif; ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment