I'm using this to show shortlinks on single post pages:
<?php echo wp_get_shortlink(); ?>
...and it works fine. However on custom post type pages it outputs nothing.
Any idea why?
Kailey Lampert answers:
Are you passing any parameters? It uses 'post' be default, but you can pass a post type as the second parameter
wp_get_shortlink( $post_id, $post_type );
Jason Manheim comments:
I have this:
function bitly_short() {
if (is_single()) { ?>
<p class="bitly-shortlink">Post Short Link: <input type="text" value="<?php echo wp_get_shortlink(); ?>" onclick="this.focus();this.select();"> <small>(click to copy)</small></p>
<?php }
}
add_action('thesis_hook_after_post','bitly_short', '0');
...how would I change that to make sure it works on both single posts and custom post type posts called "client" ?
Kailey Lampert comments:
Try this
function bitly_short() {
if (is_single()) {
global $post; ?>
<p class="bitly-shortlink">Post Short Link: <input type="text" value="<?php echo wp_get_shortlink($post->ID, $post->post_type); ?>" onclick="this.focus();this.select();"> <small>(click to copy)</small></p>
<?php }
}
add_action('thesis_hook_after_post','bitly_short', '0');
Jason Manheim comments:
Tried that, doesn't work.
Kailey Lampert comments:
Looks like I misread the info about the parameters anyway, 2nd parameter isn't post type (though the default is post). Another possible parameter is 'query'
<?php echo wp_get_shortlink($post->ID, 'query'); ?>
I'm calling it a night in just a few minutes though, so if that doesn't work for you, hopefully someone else can step up.
Navjot Singh answers:
Shortlinks don't work on Pages or any Custom Post types as of now. There is a [[LINK href="http://core.trac.wordpress.org/ticket/14760"]]bug for this in the Wordpress Trac[[/LINK]]. Unless they fix it, its impossible to achieve it without hacking the core Wordpress files.
If you still want to achieve it and is comfortable in hacking the core files, apply this patch: [[LINK href="http://core.trac.wordpress.org/attachment/ticket/14760/wp_get_shortlink-3.diff"]]http://core.trac.wordpress.org/attachment/ticket/14760/wp_get_shortlink-3.diff[[/LINK]]
Jason Manheim comments:
Ah, both good and bad news. Thank you, sir.
Romel Apuya answers:
function bitly_short() {
if (is_single() || is_singular() ) { ?>
<p class="bitly-shortlink">Post Short Link: <input type="text" value="<?php echo wp_get_shortlink(); ?>" onclick="this.focus();this.select();"> <small>(click to copy)</small></p>
<?php }
}
add_action('thesis_hook_after_post','bitly_short', '0');
try adding is_singular in the conditional statement