0

I have the following code in my functions.php file to display post shortlink in my posts (I prefer not to edit theme files if possible)...

function add_post_content($content) {
    if ( is_single() ) {
        $content .= '<p>Shortlink: ' .wp_get_shortlink(); '</p>';

    } elseif ( 'portfolio' == get_post_type() ) {
        $content .= '<p></p>';

}
    return $content;
}
add_filter('the_content', 'add_post_content');

The wp_get_shortlink function doesn't work with custom post types (I understand). I have 1 custom post type and so I am trying to stop it running on that post type but is does - just displays 'Shortlink: ' (because the function doesn't work it doesn't actually display anything else). Can anyone help me with either a way to display the custom post shortlink or edit my code to stop it showing the words 'Shortlink: '?

As an aside, would it be possible to edit the code so that the shortlink is displayed in a form textbox? I tried but couldn't get it working. Many thanks.

I've left what I have active. You'll see the output at the bottom of the pages. The second link is is the custom post type...

http://skipology.com/iphoneography-feature-puddles-featured-by-eyeem/

http://skipology.com/project/iphoneography-exhibition-hip-2b-square/

2 Answers 2

0

Reorder the logic, and fix the minor syntax error in your appended string:

function add_post_content($content) {
    if ( 'portfolio' == get_post_type() )
        return $content;

    if ( is_single() )
        return $content . '<p>Shortlink: ' . wp_get_shortlink() . '</p>';

    return $content;
}
1
  • Thank you @toscho there's no way I'd have got there myself I don't think. Works a treat. Really appreciate your help.
    – Paul Brown
    Commented Jan 6, 2013 at 14:24
1

This really helped me, I combined this with other code I had - I needed to give image links their own class for Magnific Popup, BUT only for my Blog/news section and not custom posts.

Hope this helps others:

add_filter('the_content', 'my_addlightboxrel');
function my_addlightboxrel($content) {
     if ( 'craft' == get_post_type() )
        return $content;

    if ( is_single() )
       global $post;
       $pattern ="/<a(.*?)href=('|\")(.*?).(bmp|gif|jpeg|jpg|png)('|\")(.*?)>/i";
       $replacement = '<a$1href=$2$3.$4$5 class="inline-image"$6>';
       $content = preg_replace($pattern, $replacement, $content);
       return $content;
}
1
  • Why do you need global $post;?
    – s_ha_dum
    Commented Dec 9, 2013 at 16:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.