Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Add the following lines to the end of existing add_custom_taxonomies() function declaration
- add_rewrite_tag('%location%', '^([^&/]+)', 'location=');
- add_rewrite_rule('^([^/]*)/([^/]*)/?','index.php?location=$matches[1]&name=$matches[2]','top');
- *
- * The permalink structure must be similar to http://domain.com/%location%/%postname%/
- *
- * Always go to Settings > Permalinks and click Save Changes when altering any rewrite code
- *
- * Additionally, all pages MUST be children of either home page named "benelux" or "worldwide"
- * in addition to having a similar term assigned from the "location" taxonomy
- */
- //strip page query vars from cat and tag requests, allow region home pages
- add_filter('request', 'tg_show_query_vars');
- function tg_show_query_vars( $vars ) {
- // All regional home page slugs must be in this array
- $regions = array('benelux', 'worldwide',);
- if ( array_key_exists('location', $vars ) && in_array( $vars['location'], $regions ) &&
- (( array_key_exists('category_name', $vars ) && ''!= $vars['category_name']) ||
- ( array_key_exists('tag', $vars ) && ''!= $vars['tag']))
- ) {
- unset( $vars['location']);
- }
- // Handle region home page requests
- if ( ! array_key_exists('pagename', $vars ) && ! array_key_exists('name', $vars ) &&
- array_key_exists('location', $vars ) && in_array( $vars['location'], $regions )
- ) {
- $vars['pagename'] = $vars['location'];
- unset( $vars['location']);
- }
- return $vars;
- }
- // add region term to main queries
- add_action('pre_get_posts', 'tg_set_region');
- function tg_set_region( $query ) {
- if ( get_option('page_on_front') == $query->get('page_id')) return;
- if ( !is_admin() && $query->is_main_query()) {
- if (''== $query->get('post_type') && ''== $query->get('tag') && ''== $query->get('category_name'))
- $query->set('post_type', array('post', 'page'));
- global $region;
- $explode = explode('/', $_SERVER['REQUEST_URI']);
- $region = $explode[1];
- $location = $query->get('location');
- if (''== $location) $query->set('location', $region );
- }
- }
- // go to region home page if a home page request from same site
- // region determined from referrer URL
- /* ***Functionality disabled***
- add_action('init', 'tg_home_by_region');
- function tg_home_by_region( $query ) {
- if ( array_key_exists('HTTP_REFERER', $_SERVER ) && 0 === strpos( $_SERVER['HTTP_REFERER'], site_url('/'))) {
- $explode = explode('/', $_SERVER['HTTP_REFERER']);
- $region = $explode[3];
- if ( "/$region/" != $_SERVER['REQUEST_URI'] && '/' == $_SERVER['REQUEST_URI']) {
- wp_redirect( site_url("/$region/"));
- exit;
- }
- }
- }
- */
- // Assign value to %location% rewrite tag - doesn't work for pages
- add_filter( 'post_link', 'tg_filter_post_link', 10, 2 );
- function tg_filter_post_link( $permalink, $post ) {
- // bail if %location% tag is not present in the url:
- if ( false === strpos( $permalink, '%location%'))
- return $permalink;
- $terms = wp_get_post_terms( $post->ID, 'location');
- // set location, if no location is found, provide a default value.
- if ( 0 < count( $terms ))
- $location = $terms[0]->slug;
- else
- $location = 'benelux'; // this is the default value
- $location = urlencode( $location );
- $permalink = str_replace('%location%', $location , $permalink );
- return $permalink;
- }
- // alter category and tag links to be compatible with global $region category
- add_filter('term_link', 'tg_alter_term_links', 999, 3 );
- function tg_alter_term_links( $termlink, $term, $taxonomy ) {
- global $region;
- if ('category' == $taxonomy ) {
- $termlink = site_url('/') . $region . '?category_name=' . $term->slug;
- }
- if ('post_tag' == $taxonomy ) {
- $termlink = site_url('/') . $region . '?tag=' . $term->slug;
- }
- return $termlink;
- }
- /**
- * Outputs prev/next nav links for custom query pages
- *
- * @param array, $args, the same $args used for the page's main WP_Query
- * The args array must include an 'offset' argument, relying on 'paged' will not work.
- * string, $next, text for next page link (optional) default "Older Posts"
- * string, $prev, text for previous page link (optional) default "Newer Posts"
- * @return none, outputs prev/next nav links with region_page URL parm inside a div with
- * class 'search_page_nav', anchor links have class 'search_page_next' and 'search_page_prev'
- * @globals $_GET['region_page'], used to determine current page number. Page 1 assumed
- * if 'region_page' is not set.
- */
- function tg_custom_query_nav( $args, $next = 'Older Posts', $prev = 'Newer Posts' ) {
- // determine pagination parms
- $s_page = isset( $_GET['region_page']) ? sanitize_text_field( $_GET['region_page']) : 1;
- $pppage = isset( $args['posts_per_page']) ? $args['posts_per_page'] : get_option('posts_per_page');
- if ( -1 == $pppage ) return;
- $prev_off = ( $s_page - 2 ) * $pppage;
- $next_off = $s_page * $pppage;
- // check for more posts
- $args['no_found_posts'] = true; // just get a count
- $args['ignore_sticky_posts'] = true;
- $args['offset'] = $next_off;
- $query = new WP_Query( $args ); // get next page post count
- $ct_next = $query->post_count;
- $args['offset'] = $prev_off;
- $query = new WP_Query( $args ); // get prev page post count
- $ct_prev = 1 < $s_page ? $query->post_count : 0;
- // output links
- echo "\n<div class=\"paging-navigation\"><div class=\"nav-links\">";
- $link = remove_query_arg('region_page');
- $next .= '<i class="fa fa-chevron-right" aria-hidden="true"></i>';
- $nxt_link = esc_url( add_query_arg('region_page', $s_page+1, $link ));
- if ( $ct_next ) echo "\n<a class=\"nav-next\" href=\"$nxt_link\">$next</a>";
- $prev = '<i class="fa fa-chevron-left" aria-hidden="true"></i>'. $prev;
- $prv_link = esc_url( add_query_arg('region_page', $s_page-1, $link ));
- if ( $ct_prev ) echo "\n<a class=\"nav-previous\" href=\"$prv_link\">$prev</a>\n";
- echo "</div></div>\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement