WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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...



share|improve this question
up vote 0 down vote accepted

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;
share|improve this answer
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 Jan 6 '13 at 14:24

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;
share|improve this answer
Why do you need global $post;? – s_ha_dum Dec 9 '13 at 16:17

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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