Advertisement
Guest User

Untitled

a guest
Oct 1st, 2014
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. function register_post_assets(){
  2. add_meta_box('featured-post', __('Featured Post'), 'add_featured_meta_box', 'post', 'advanced', 'high');
  3. }
  4. add_action('admin_init', 'register_post_assets', 1);
  5.  
  6. function add_featured_meta_box($post){
  7. $featured = get_post_meta($post->ID, '_featured-post', true);
  8. echo "<label for='_featured-post'>".__('Feature this post?', 'foobar')."</label>";
  9. echo "<input type='checkbox' name='_featured-post' id='featured-post' value='1' ".checked(1, $featured)." />";
  10. }
  11.  
  12. function save_featured_meta($post_id){
  13. // Do validation here for post_type, nonces, autosave, etc...
  14. if (isset($_REQUEST['featured-post']))
  15. update_post_meta(esc_attr($post_id, '_featured-post', esc_attr($_REQUEST['featured-post'])));
  16. // I like using _ before my custom fields, so they are only editable within my form rather than the normal custom fields UI
  17. }
  18. add_action('save_post', 'save_featured_meta');
  19.  
  20. $args = array(
  21. 'meta_key' => '_featured-post', // include underscore prefix in key name
  22. 'meta_value' => 1
  23. );
  24. // The number of posts displayed would be determined under Settings->Reading
  25. query_posts($args);
  26.  
  27. if(have_posts()): while(have_posts()): the_post();
  28. // Do your bidding here
  29.  
  30. endwhile; else:
  31.  
  32. endif;
  33.  
  34. $args = array(
  35. 'posts_per_page' => 5,
  36. 'meta_key' => '_featured-post',
  37. 'meta_value' => 1
  38. );
  39.  
  40. $featured = new WP_Query($args);
  41.  
  42. if ($featured->have_posts()): while($featured->have_posts()): $featured->the_post();
  43. the_title();
  44. the_content();
  45. endwhile; else:
  46.  
  47. endif;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement