In this part of my tutorial on how to create a WordPress Featured Content Slider I’ll cover many topics.
I’ll show you how to:
// Retrieve the number of currently Featured posts
$postsFeatured = get_term_by(‘name’, ‘Featured’, ‘category’);
echo “Featured Posts: “, $postsFeatured->count, “<br /><br />”;
// Issue a custom query that outputs the titles for all featured articles
// To issue queries I need to hijack $wp_query. So, I can use it later
// on the page I’m saving it to a temporary variable
$tempWPQuery = $wp_query;
$wp_query = null;
// Create a new WP_Query Object
$wp_query = new WP_Query();
// Issue a query that returns all posts in the category Featured
$wp_query->query(‘posts_per_page=’.$postsFeatured->count.’&category_name=Featured’);
// Cycle through all of the posts
while ($wp_query->have_posts()):
$wp_query->the_post();
// Output the title of the post
the_title();
echo “<br /><br />”;
// Retrieve the value of the excerpt and trim it to 330 characters
$ntt_long_excerpt = get_the_excerpt();
$ntt_short_excerpt = substr($ntt_long_excerpt, 0, 330);
echo $ntt_short_excerpt . “…<br /><br />”;
// Output the link url for the post
the_permalink();
echo “<br /><br />”;
// Retrieve the url for the featured image which is saved as a meta value
$ntt_featured_image = get_post_meta( $post->ID, ‘_ntt_mbe_image’, true );
echo $ntt_featured_image . “<br /><br />”;
// Output the directory path for my plugin
echo plugin_dir_path(__FILE__). “<br/>”;
// Output the directory path for my theme
echo bloginfo(‘template_directory’);
?> <!– Run the Tom Thumb script to crop and shrink my featured image –>
<div class=”featured_thumb”>
<img src=”<?php bloginfo(‘template_directory’); ?>/timthumb/timthumb.php?src=<?php echo $ntt_featured_image; ?>&h=48&w=48&zc=1″ alt=”<?php the_title(); ?>” width=”48″ height=”48″ />
</div>
<?php
endwhile;
// Reset wp_query
$wp_query = null;
$wp_query = $tempWPQuery;
Leave a Reply