Thoughts on Design, Code, Technology, and Architecture.
67a2 is the work of Cambridge, MA designer Paul Voulgaris.
-
search
-
tags
via twitter @67a2
- 14.May 2:00am
With Color Manipulation, Photographer Creates Minimalist Photographs – http://t.co/CIN7TYu2 http://t.co/xhFVaEja via @sharethis
- 01.May 12:22am
“Working in Mumbai” – client Rahul Mehrotra will lecture at Harvard this friday. http://t.co/2GgBIqlp
- 30.Apr 2:59pm
Patterned by Nature Sculptural Transparent Display video: http://t.co/Sj4oQjJ5 collabora http://t.co/P6SMY5pf
- 27.Apr 11:26am
Goodbye SketchUp? – Google Is Getting Out Of The 3D Modeling Business – http://t.co/5SDIbrX9
- 13.Apr 2:36pm
hopefully Jony Ive has final say, or we will end up with neon iMacs again: Apple & Philippe Starck http://t.co/pf2a3lKy via @techcrunch
- 05.Apr 1:33pm
Very excited for the (Sat?) opening of @Barismo‘s new coffee bar #dwelltime .. right across the street! http://t.co/bzmHzaPv
tag: code
WordPress: order 5 categories by the most recent posts
Friday, March 18th, 2011
A client site has 5 main categories that make up it’s mission. All posts are organized by these 5.
We want the category of the most recent post to be at the top of the list, followed by 3 posts from it.
Here’s my effort at this simple enough sounding problem.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | <!--?php /////////////// CREATE A CATEGORY ARRAY /////////////// $cat_array = array(); $args=array( 'post_type' =--> 'post', 'post_status' => 'publish', 'posts_per_page' =>10 //number of posts to collect from... could be higher, I chose 10 ); $my_query = null; $my_query = new WP_Query($args); if( $my_query->have_posts() ) { while ($my_query->have_posts()) : $my_query->the_post(); $cat_args=array('orderby' => 'none'); $cats = wp_get_post_terms( $post->ID , 'category', $cat_args); foreach($cats as $cat) { $cat_array[$cat->term_id] = $cat->term_id; } endwhile; } // remove categories that are not the main 5 //I want these 5 specific cats to show - their category ID's are 31,32,33,34,35 if ($cat_array) { foreach($cat_array as $cat) { $category = get_term_by('ID',$cat, 'category'); if ( $category->term_id == 31 || $category->term_id == 32 || $category->term_id == 33 || $category->term_id == 34 || $category->term_id == 35 ){ }else{ unset($cat_array[$category->term_id]); } } } //make sure all of the 5 main categories are in the 10 test posts //otherwise I'll add them to the end of the array (hack) if (!in_array(31, $cat_array)) { $cat_array[31] = 31; } if (!in_array(32, $cat_array)) { $cat_array[32] = 32; } if (!in_array(33, $cat_array)) { $cat_array[33] = 33; } if (!in_array(34, $cat_array)) { $cat_array[34] = 34; } if (!in_array(35, $cat_array)) { $cat_array[35] = 35; } /////////////// GET POSTS FOR EACH CATEGORY /////////////// // GET THE CATEGORY if ($cat_array) { foreach($cat_array as $cat) { $category = get_term_by('ID',$cat, 'category'); $args=array( 'cat' => $category->term_id, 'showposts'=>3, //number of posts to show 'caller_get_posts'=>1 ); $post_count = 1; //reset post count to 1 // GET THE POSTS $my_query = new WP_Query($args); if( $my_query->have_posts() ) { while ($my_query->have_posts()) : $my_query->the_post(); //////////////////////////////////////// YOUR CATEGORY HERE if( $post_count == 1){ echo '' . $category->name .''; } //////////////////////////////////////// YOUR POSTS HERE echo the_title() .''; $post_count++; endwhile; } wp_reset_query(); } } wp_reset_query(); ?> |











Contact Us
Main Site: 67a2.com
Facebook: facebook.com/67a2media
Twitter: twitter.com/67a2
Subscribe to our RSS feed