Last active
December 26, 2015 09:39
-
-
Save Ikaring/7131427 to your computer and use it in GitHub Desktop.
Shortcode to fetch permalink by slug for page, category, post
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 | |
/** | |
* Shortcode to fetch permalink by slug for page, category, post | |
* | |
* [permalink type='cat' slug='hoge'] | |
* | |
* @param type :page(default), cat, post | |
* @param slug :only child page needs parent slug. eg.slug='parent/child' | |
* | |
* @return url string | |
*/ | |
function fetch_permalink( $atts ) { | |
extract( shortcode_atts( array( | |
'type' => 'page', | |
'slug' => '', | |
), $atts ) ); | |
if ( $type == 'page' ){ | |
$link = get_permalink( get_page_by_path( $slug ) ); | |
} elseif ( $type == 'cat' ){ | |
$catObj = get_category_by_slug( $slug ); | |
$link = get_category_link( $catObj->term_id ); | |
} elseif ( $type == 'post' ){ | |
$link = get_permalink( get_page_by_path( $slug, OBJECT, 'post' ) ); | |
} | |
return $link ; | |
} | |
add_shortcode('permalink', 'fetch_permalink'); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment