Created
November 1, 2013 10:49
-
-
Save nikolov-tmw/7263782 to your computer and use it in GitHub Desktop.
This file contains 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 | |
add_action( 'init', 'register_faq_post_type' ); | |
function register_faq_post_type() { | |
register_post_type( 'faq', | |
array( | |
'labels' => array( | |
'name' => 'Frequently Asked Questions', | |
'menu_name' => 'FAQ Manager', | |
'singular_name' => 'Question', | |
'all_items' => 'All Questions' | |
), | |
'public' => true, | |
'publicly_queryable' => true, | |
'show_ui' => true, | |
'show_in_menu' => true, | |
'show_in_nav_menus' => true, | |
'menu_position ' => 20, | |
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'comments', 'post-formats', 'revisions' ), | |
'hierarchical' => false, | |
'has_archive' => 'faq', | |
'rewrite' => array( 'slug' => 'faq-item', 'hierarchical' => true, 'with_front' => false ) | |
) | |
); | |
register_taxonomy( 'faq-category', array( 'faq' ), | |
array( | |
'labels' => array( | |
'name' => 'FAQ Categories', | |
'menu_name' => 'FAQ Categories', | |
'singular_name' => 'FAQ Category', | |
'all_items' => 'All Categories' | |
), | |
'public' => true, | |
'hierarchical' => true, | |
'show_ui' => true, | |
'rewrite' => array( 'slug' => 'faq-category', 'hierarchical' => true, 'with_front' => false ), | |
) | |
); | |
} | |
?> |
This file contains 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 | |
add_action( 'generate_rewrite_rules', 'register_faq_rewrite_rules' ); | |
function register_faq_rewrite_rules( $wp_rewrite ) { | |
$new_rules = array( | |
'faq/([^/]+)/?$' => 'index.php?faq-category=' . $wp_rewrite->preg_index( 1 ), | |
'faq/([^/]+)/([^/]+)/?$' => 'index.php?post_type=faq&faq-category=' . $wp_rewrite->preg_index( 1 ) . '&faq=' . $wp_rewrite->preg_index( 2 ), | |
'faq/([^/]+)/([^/]+)/page/(\d{1,})/?$' => 'index.php?post_type=faq&faq-category=' . $wp_rewrite->preg_index( 1 ) . '&paged=' . $wp_rewrite->preg_index( 3 ), | |
'faq/([^/]+)/([^/]+)/([^/]+)/?$' => 'index.php?post_type=faq&faq-category=' . $wp_rewrite->preg_index( 2 ) . '&faq=' . $wp_rewrite->preg_index( 3 ), | |
); | |
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules; | |
} | |
// A hacky way of adding support for flexible custom permalinks | |
// There is one case in which the rewrite rules from register_kb_rewrite_rules() fail: | |
// When you visit the archive page for a child section(for example: http://example.com/faq/category/child-category) | |
// The deal is that in this situation, the URL is parsed as a Knowledgebase post with slug "child-category" from the "category" section | |
function fix_faq_subcategory_query($query) { | |
if ( isset( $query['post_type'] ) && 'faq' == $query['post_type'] ) { | |
if ( isset( $query['faq'] ) && $query['faq'] && isset( $query['faq-category'] ) && $query['faq-category'] ) { | |
$query_old = $query; | |
// Check if this is a paginated result(like search results) | |
if ( 'page' == $query['faq-category'] ) { | |
$query['paged'] = $query['name']; | |
unset( $query['faq-category'], $query['name'], $query['faq'] ); | |
} | |
// Make it easier on the DB | |
$query['fields'] = 'ids'; | |
$query['posts_per_page'] = 1; | |
// See if we have results or not | |
$_query = new WP_Query( $query ); | |
if ( ! $_query->posts ) { | |
$query = array( 'faq-category' => $query['faq'] ); | |
if ( isset( $query_old['faq-category'] ) && 'page' == $query_old['faq-category'] ) { | |
$query['paged'] = $query_old['name']; | |
} | |
} | |
} | |
} | |
return $query; | |
} | |
add_filter( 'request', 'fix_faq_subcategory_query', 10 ); | |
?> |
This file contains 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 | |
function faq_article_permalink( $article_id, $section_id = false, $leavename = false, $only_permalink = false ) { | |
$taxonomy = 'faq-category'; | |
$article = get_post( $article_id ); | |
$return = '<a href="'; | |
$permalink = ( $section_id ) ? trailingslashit( get_term_link( intval( $section_id ), 'faq-category' ) ) : home_url( '/faq/' ); | |
$permalink .= trailingslashit( ( $leavename ? "%$article->post_type%" : $article->post_name ) ); | |
$return .= $permalink . '/" >' . get_the_title( $article->ID ) . '</a>'; | |
return ( $only_permalink ) ? $permalink : $return; | |
} | |
function filter_faq_post_link( $permalink, $post, $leavename ) { | |
if ( get_post_type( $post->ID ) == 'faq' ) { | |
$terms = wp_get_post_terms( $post->ID, 'faq-category' ); | |
$term = ( $terms ) ? $terms[0]->term_id : false; | |
$permalink = faq_article_permalink( $post->ID, $term, $leavename, true ); | |
} | |
return $permalink; | |
} | |
add_filter( 'post_type_link', 'filter_faq_post_link', 100, 3 ); | |
function filter_faq_section_terms_link( $termlink, $term, $taxonomy = false ) { | |
if ( $taxonomy == 'faq-category' ) { | |
$section_ancestors = get_ancestors( $term->term_id, $taxonomy ); | |
krsort( $section_ancestors ); | |
$termlink = home_url( '/faq/' ); | |
foreach ( $section_ancestors as $ancestor ) { | |
$section_ancestor = get_term( $ancestor, $taxonomy ); | |
$termlink .= $section_ancestor->slug . '/'; | |
} | |
$termlink .= trailingslashit( $term->slug ); | |
} | |
return $termlink; | |
} | |
add_filter( 'term_link', 'filter_faq_section_terms_link', 100, 3 ); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment