Skip to content

Instantly share code, notes, and snippets.

@danielck
Created August 27, 2015 12:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielck/d3a05a961c754a1e2480 to your computer and use it in GitHub Desktop.
Save danielck/d3a05a961c754a1e2480 to your computer and use it in GitHub Desktop.
<?php
/**
* Returns the localised slug for a taxonomy term
*/
function h1_term_slug( $slug, $taxonomy, $return_original_if_missing = true ) {
$localized_term = h1_get_term_by( 'slug', $slug, $taxonomy, OBJECT, 'raw', $return_original_if_missing );
if ( $localized_term && !is_wp_error( $localized_term ) ) {
$slug = $localized_term->slug;
} elseif ( $return_original_if_missing == false ) {
$slug = false;
}
return $slug;
}
/**
* Returns the localised slug for a page by slug
*/
function h1_page_slug( $path ) {
$page = h1_get_page_by_path( $path );
if ( !$page )
return false;
return $page->post_name;
}
/**
* Returns the localised page by slug
*/
function h1_get_page_by_path( $path ) {
// get page id
$page = get_page_by_path( $path );
if ( !$page )
return false;
$localized_id = h1_get_localized_post_id( $page->ID );
return get_post( $localized_id );
}
function h1_get_localized_post_id( $post_id ) {
if ( function_exists( 'pll_get_post' ) && 'fi' != h1_current_language() ) {
$localized_post_id = pll_get_post( $post_id );
if ( $localized_post_id ) return $localized_post_id;
}
return $post_id;
}
/**
* Always have a current language
*/
function h1_current_language() {
if ( function_exists( 'pll_current_language' ) ) {
return pll_current_language();
}
/**
* Default if polylang is not active
*/
return 'fi';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment