Skip to content

Instantly share code, notes, and snippets.

@inakagawa
Last active May 17, 2016 02:08
Show Gist options
  • Save inakagawa/6d0f3cb6d3888d2fd489b1a4c3bef7df to your computer and use it in GitHub Desktop.
Save inakagawa/6d0f3cb6d3888d2fd489b1a4c3bef7df to your computer and use it in GitHub Desktop.
WordPress: Permalink from slug - shortcode
<?php
function sc_getpermalink_by_path($args){
$atts = shortcode_atts(
array(
'slug' => '',
'post_type' => 'page',
'ret_type' => 'OBJECT'
),
$args, 'sc_path');
$pageobj = get_page_by_path($atts['slug'], $atts['ret_type'], $atts['post_type']);
$fullpath = '';
if($pageobj){
$id = $pageobj->ID;
$fullpath = get_permalink($id);
}else{
$fullpath = get_home_url();
}
return $fullpath;
}
add_shortcode('sc_path', 'sc_getpermalink_by_path');
@inakagawa
Copy link
Author

使い方

[sc_path slug="foobar" post_type="post"]
[sc_path slug="barbar"]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment