WordPress Featured Content Pt 7

Wordpress Featured Content 7In this part of the tutorial, I continue making a WordPress Featured Content Slider.

I’ll specifically show you how to do many things with WordPress like:

  • Find out what category a post is in
  • Get the URL for the Plugin Directory
  • Retrieve Post Titles, Excerpts, Meta Data
  • Make custom queries for the WordPress database
All of the code follows the video. It is heavily commented. If you somehow got here without seeing the beginning of this tutorial see WordPress Featured Content Pt 1.

Code From the Video

<?php

 

// Get an object that contains all posts in the category Featured

$postsFeatured = get_term_by(‘name’, ‘Featured’, ‘category’);

 

// if( in_category(‘Featured’) AND ($ntt_posts_featured > 5) ){

 

// Make sure I only retrieve a maximum of 15 posts

$ntt_posts_featured = (($postsFeatured->count) > 15) ? 15 : ($postsFeatured->count);

 

// Call the function that will create the featured content

ntt_make_fc_pages($ntt_posts_featured);

 

 

}

?>

 

<?php

// This function creates all of the featured content code

function ntt_make_fc_pages($ntt_num_fc_pages) {

 

// Figure out how many featured content pages I should make

// knowing I need a minimum of 5 posts per page

$ntt_fc_pages = floor($ntt_num_fc_pages / 5);

 

$ntt_fc_info = array();

 

// Get the url for the plugin

$ntt_plugin_directory = WP_PLUGIN_URL.’/’.str_replace(basename( __FILE__),””,plugin_basename(__FILE__));

 

// 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();

 

// Get the total number of posts to retreive

$postsFeatured = $ntt_fc_pages * 5;

 

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

 

$ntt_post_num = 1;

while ($wp_query->have_posts()):

$wp_query->the_post();

 

// Output the title of the post

$ntt_fc_info[$ntt_post_num][0] = get_the_title($post->ID);

 

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

$ntt_fc_info[$ntt_post_num][1] = $ntt_short_excerpt;

 

// Output the link url for the post

$ntt_fc_info[$ntt_post_num][2] = get_permalink($post->ID);

 

// Retrieve the url for the featured image which is saved as a meta value

$ntt_post_id = get_the_ID();

$ntt_featured_image = get_post_meta( $ntt_post_id, ‘_ntt_mbe_image’, true );

$ntt_fc_info[$ntt_post_num][3] = $ntt_featured_image;

 

$ntt_fc_info[$ntt_post_num][4] = ‘<img src=”‘ . bloginfo(‘template_directory’);

 

$ntt_fc_info[$ntt_post_num][4] .= ‘/timthumb/timthumb.php?src=’ . $ntt_featured_image;

$ntt_fc_info[$ntt_post_num][4] .= ‘&h=48&w=48&zc=1″ alt=”‘ . $ntt_fc_info[$ntt_post_num][0];

$ntt_fc_info[$ntt_post_num][4] .= ‘” width=”48″ height=”48″ title=”‘ . $ntt_fc_info[$ntt_post_num][0] . ‘” /></div>’;

 

$ntt_post_num++;

endwhile;

Leave a Reply

Your email address will not be published.

Google+