Last active
August 29, 2015 14:23
-
-
Save badcrocodile/a8a30984b75ae00cc894 to your computer and use it in GitHub Desktop.
Custom slugs and query string parameters in WordPress URL's
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 | |
| /** | |
| * This is an example of a custom loop with pagination and a custom URL structure | |
| * get_year and get_month are our custom query string variables | |
| * EX: http://saos.co/clevelandplus/public/news-press/cle-in-the-national-press/2014/09/page/3/ | |
| * where /2014/ and /09/ represent our custom query string parameters | |
| */ | |
| global $wp_query; | |
| if(isset($wp_query->query_vars['getyear'])) { | |
| $get_year = $wp_query->query_vars['getyear']; | |
| } | |
| if(isset($wp_query->query_vars['getmonth'])) { | |
| $get_month = $wp_query->query_vars['getmonth']; | |
| } | |
| $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1; | |
| $args = array( | |
| 'post_type' => 'national-press', | |
| 'posts_per_page' => 4, | |
| 'paged' => $paged | |
| ); | |
| $query = new WP_Query($args); | |
| // hack to get pagination to work on custom post type custom query | |
| $temp_query = $wp_query; | |
| $wp_query = NULL; | |
| $wp_query = $query; ?> | |
| <?php if($query->have_posts()) : ?> | |
| <?php while($query->have_posts()) : $query->the_post(); ?> | |
| <h3><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3> | |
| <div class="date"><?php the_date(); ?></div> | |
| <?php the_excerpt(); ?> | |
| <hr /> | |
| <?php endwhile; ?> | |
| <?php endif; ?> | |
| <?php // wp_reset_query(); ?> | |
| <?php // wp_reset_postdata(); ?> | |
| <div class="pagination"> | |
| <span class="previous"><?php previous_posts_link(__('« Previous', 'carrington-jam')) ?></span> | |
| <span class="next"><?php next_posts_link(__('Next »', 'carrington-jam')) ?></span> | |
| </div> | |
| <?php | |
| // Undo our query hack | |
| $wp_query = NULL; | |
| $wp_query = $query; | |
| ?> |
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 | |
| /* ========================= */ | |
| /* == Configure our routes == */ | |
| /* ========================= */ | |
| // For month and/or year based archives | |
| function saos_rewrite_tags() { | |
| add_rewrite_tag('%getyear%', '([^&]+)'); | |
| add_rewrite_tag('%getmonth%', '([^&]+)'); | |
| } | |
| add_action('init', 'saos_rewrite_tags', 10, 0); | |
| function saos_rewrites() { | |
| /* -- CLE+ in the National Press (page id 41) -- */ | |
| // for root URL (no month/year) pagination | |
| add_rewrite_rule('^news-press/national-press/page/([0-9]{1,})/?$','index.php?page_id=41&paged=$matches[1]','top'); | |
| // for year and month archives, ie: http://foo.com/bar/2013/02/ | |
| add_rewrite_rule('^news-press/national-press/([^/]*)/([^/]*)/?$','index.php?page_id=41&getyear=$matches[1]&getmonth=$matches[2]','top'); | |
| // and for month/year with pagination, ie: http://foo.com/bar/2013/02/page/3/ | |
| add_rewrite_rule('^news-press/national-press/([^/]*)/([^/]*)/page/([0-9]{1,})/?$','index.php?page_id=41&getyear=$matches[1]&getmonth=$matches[2]&paged=$matches[3]','top'); | |
| // for just year archives, ie: http://foo.com/bar/2014/ | |
| add_rewrite_rule('^news-press/national-press/([^/]*)/?$','index.php?page_id=41&getyear=$matches[1]','top'); | |
| // and for year with pagination, ie: http://foo.com/bar/2013/page/3/ | |
| add_rewrite_rule('^news-press/national-press/([^/]*)/page/([0-9]{1,})/?$','index.php?page_id=41&getyear=$matches[1]&paged=$matches[2]','top'); | |
| } | |
| add_action('init', 'saos_rewrites', 10, 0); | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment