$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>functions - Can't properly set the_title add_filter to show short_URL|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

functions - Can't properly set the_title add_filter to show short_URL

matteradmin9PV0评论

I am trying to show Bitly's Post short URLs for my blog posts but something seems off as when I am using this code, I see the output all over the page for menu items etc...

Additionally, the URL doesn't seem to be outputted correctly. After many attempts, I am trying to use this:

function shorturl_after_title( $title ) {
    if ( function_exists('wp_get_shortlink') && is_single() && 'post' == get_post_type() ) {

            $aftertitle = "<br><span class='post-shortlink'>رابط مختصر:
<input type='text' value='<?php echo wp_get_shortlink(get_the_ID()); ?>' onclick='this.focus(); this.select();' />
</span>";           
            $custom_title = $title . $aftertitle;
            return $custom_title;
    }
}
add_filter( 'the_title', 'shorturl_after_title' );

Can you please help with this? Thanks!

I am trying to show Bitly's Post short URLs for my blog posts but something seems off as when I am using this code, I see the output all over the page for menu items etc...

Additionally, the URL doesn't seem to be outputted correctly. After many attempts, I am trying to use this:

function shorturl_after_title( $title ) {
    if ( function_exists('wp_get_shortlink') && is_single() && 'post' == get_post_type() ) {

            $aftertitle = "<br><span class='post-shortlink'>رابط مختصر:
<input type='text' value='<?php echo wp_get_shortlink(get_the_ID()); ?>' onclick='this.focus(); this.select();' />
</span>";           
            $custom_title = $title . $aftertitle;
            return $custom_title;
    }
}
add_filter( 'the_title', 'shorturl_after_title' );

Can you please help with this? Thanks!

Share Improve this question asked Dec 31, 2018 at 20:29 Dr.HaririDr.Hariri 731 silver badge12 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

There's a second argument passed to the_title filter, which is the ID of the post the filter is currently operating on. This is important, as you've discovered, because the filter runs any time a title is output- in a menu item, widget, any secondary query, etc., so you need to check if that post ID matches the ID of the post you're currently viewing.

Your other problem is a syntax issue where you set $aftertitle. You've got php open/close braces and an echo inside there where you should be using string concatenation, like the way you join $title . $aftertitle on the next line. Anyway, we can do that a bit differently.

So here's a new version of your function. We can combine the single and post type checks into one, and I think it's safe to assume that wp_get_shortlink exists in this context. We then use php sprintf to generate the string, this is just a personal preference.

function shorturl_after_title( $title, $post_id ) {
    if ( is_singular( 'post' ) && $post_id == get_queried_object_id() ) {
        $text = "%s<br><span class='post-shortlink'>رابط مختصر:<input type='text' value='%s' onclick='this.focus(); this.select();' /></span>";           
        $title = sprintf( $text, $title, wp_get_shortlink( $post_id ) );
    }
    return $title;
}
add_filter( 'the_title', 'shorturl_after_title', 10, 2 );
Post a comment

comment list (0)

  1. No comments so far