Wordpress "Featured" Posts


/ Published in: PHP
Save to your folder(s)

This can be dropped into either index.php or any custom page template to display posts in the "Featured" category.

Alternatively you could create a custom loop (loop-featured.php, for example) using this query.


Copy this code and paste it in your HTML
  1. <div id="featured-posts">
  2. <?php
  3. $featured_query = "
  4. SELECT wposts.*
  5. FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
  6. WHERE wposts.ID = wpostmeta.post_id
  7. AND wpostmeta.meta_key = 'featured'
  8. AND wpostmeta.meta_value = 'yes'
  9. AND wposts.post_type = 'post'
  10. ";
  11. $featured = $wpdb->get_results($featured_query, OBJECT);
  12. ?>
  13.  
  14. <?php if ($featured): ?>
  15. <?php foreach ($featured as $post): ?>
  16. <?php //print_r($post); ?>
  17. <div class="post" id="post-<?php echo $post->ID ?>">
  18. <a href="<?php the_permalink() ?>">
  19. <div class="thumbnail">
  20. <?php echo get_the_post_thumbnail($post->ID, 'header-thumbnail'); ?>
  21. <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h2>
  22. </div>
  23. </a>
  24.  
  25. <small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>
  26.  
  27. <div class="entry">
  28. <?php echo $post->post_excerpt; ?>
  29. </div>
  30.  
  31. <p class="postmetadata">Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?>
  32. <?php comments_popup_link('No Comments &raquo;', '1 Comment &raquo;', '% Comments &raquo;'); ?></p>
  33. </div>
  34. <?php endforeach; ?>
  35.  
  36. <?php else : ?>
  37. <h2 class="center">Not Found</h2>
  38. <p class="center">Sorry, but you are looking for something that isn't here.</p>
  39. <?php include (TEMPLATEPATH . "/searchform.php"); ?>
  40. <?php endif; ?>
  41.  
  42. </div>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.