Skip to content

Instantly share code, notes, and snippets.

@billerickson
Created December 18, 2011 00:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save billerickson/1491863 to your computer and use it in GitHub Desktop.
Save billerickson/1491863 to your computer and use it in GitHub Desktop.
Rewrite Rules
<?php
/**
* Rewrite Rules
*
* @package BE_Genesis_Child
* @author Bill Erickson <bill@billerickson.net>
* @copyright Copyright (c) 2011, Bill Erickson
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
*/
/**
* Generate Rewrite Rules for Lifestyle taxonomy and single posts
* @author Bill Erickson
*
* Ex: example.com/lifestyle/section
* Ex: example.com/lifestyle/section/post-name
*
*/
function be_rewrite_rules() {
// Taxonomy with Pagination
add_rewrite_rule(
'lifestyle/([^/]+)/page/([^/]+)/?',
'index.php?lifestyle-section=$matches[1]&paged=$matches[2]',
'top'
);
// Single Post
add_rewrite_rule(
'lifestyle/([^/]+)/([^/]+)/?',
'index.php?lifestyle=$matches[2]&post_type=lifestyle&name=$matches[2]',
'top'
);
// Taxonomy
add_rewrite_rule(
'lifestyle/([^/]+)/?',
'index.php?lifestyle-section=$matches[1]',
'top'
);
}
add_action( 'init', 'be_rewrite_rules' );
/**
* Update Links for Lifestyle Section taxonomy
* See get_term_link() in wp-includes/taxonomy.php
* @author Bill Erickson
*
* @param string $termlink
* @param object $term
* @param string $taxonomy
* @return string $termlink
*/
function be_lifestyle_section_term_link( $termlink, $term, $taxonomy ) {
if( 'lifestyle-section' !== $taxonomy )
return $termlink;
$termlink = trailingslashit( get_bloginfo( 'url' ) ) . 'lifestyle/' . $term->slug . '/';
return $termlink;
}
add_filter( 'term_link', 'be_lifestyle_section_term_link', 10, 3 );
/**
* Update Links for Lifestyle Section single posts
* See get_post_permalink() in wp-includes/link-template.php
* @author Bill Erickson
*
* @param string $link
* @param object $post
* @param bool $leavename
* @param string $sample
* @return string
*/
function be_lifestyle_section_single_link( $link, $post, $leavename, $sample ) {
if( 'lifestyle' !== $post->post_type )
return $link;
$section = wp_get_object_terms( $post->ID, 'lifestyle-section', array( 'count' => '1' ) );
$section_slug = isset( $section[0]->slug ) ? $section[0]->slug : 'articles';
$page_slug = $sample ? '%postname%': $post->post_name;
$link = trailingslashit( get_bloginfo( 'url' ) ) . 'lifestyle/' . trailingslashit( $section_slug ) . trailingslashit( $page_slug );
return $link;
}
add_filter( 'post_type_link', 'be_lifestyle_section_single_link', 10, 4 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment