WordPress Featured Content Pt 6

Wordpress Featured Content 6In 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:

  • Count the Number of Posts in a Category
  • Create Custom WordPress Queries
  • Pull Post Titles, Excerpts, Links, etc.
  • Use Tim Thumb to Shrink Images
All of the code used in this tutorial follows the video. Use it however you’d like. I also show you how to use Tim Thumb below and provide the script.

How to Use Tim Thumb
I use the script Tim Thumb to turn my featured content image into a thumbnail. You can get the script here Tim Thumb Script. Just copy the script and change the extension from txt to php. Then follow these steps:
  • Save the script in a folder called timthumb
  • Create a folder named cache in that same folder
  • Save the timthumb folder in your main theme folder in WordPress
  • Change permissions on the cache and timthumb folder to 777
To turn an image into a thumbnail you can execute timthumb by passing it the image to shrink, height, weight and give a variable named zc the value of 1 like this.
bloginfo(‘template_directory’); ?>/timthumb/timthumb.php?src=<?php echo $ntt_featured_image; ?>&h=48&w=48&zc=1″
  • bloginfo(‘template_directory’); : Returns the directory for your theme
  • h : is a reference to height
  • w : is a reference to width
  • zc : If set to 1 the image will be cropped, if 0 it won’t be
I talk about Tim Thumb further in the video above.
Code from the Video

// 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

Your email address will not be published.

Google+