Created
August 10, 2013 21:57
-
-
Save ryelle/6202353 to your computer and use it in GitHub Desktop.
Code example for this Forrst post: http://forrst.com/posts/A_more_elegant_way_of_doing_this_PHP-GCM
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Use WP_Query, not query_posts. | |
| * get_posts is an OK alternative, but is basically a | |
| * wrapper for WP_Query; and using WP_Query directly lets us | |
| * use `$my_query->the_post();` which sets up the global $post | |
| * object -- so that we can use the `the_*` functions. | |
| * http://codex.wordpress.org/Class_Reference/WP_Query#Usage | |
| */ | |
| $my_query = new WP_Query(array( | |
| 'cat' => '21', | |
| 'orderby' => 'CPTOrderPosts' | |
| )); | |
| while ( $my_query->have_posts() ): $my_query->the_post(); | |
| /** | |
| * Use post_class to output the classes so that you also get | |
| * all the classes WP attaches to a post, like the post-type, | |
| * categories, format... | |
| * http://codex.wordpress.org/Function_Reference/post_class | |
| * | |
| * `$my_query->current_post` is kept track of in the query itself, | |
| * so no need to create a `$i`. | |
| */ ?> | |
| <li id="wh-<?php the_ID(); ?>" <?php post_class( 'post-' . $my_query->current_post ); ?>> | |
| <a href="<?php the_permalink();?>"><?php the_title(); ?></a> | |
| <?php the_content(); ?> | |
| </li> | |
| <?php endwhile; | |
| /** | |
| * When you're done with the $my_query info, reset the global post. | |
| * This sets the $post object back to whatever page you're on, so | |
| * the `the_*` functions will return the main query's info. | |
| */ | |
| wp_reset_postdata(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment