$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'); ?>How to Get a part of URL and put in shortcode?|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)

How to Get a part of URL and put in shortcode?

matteradmin9PV0评论

I have this shortcode

[broo_user_badges username=""]

and I have to put username between " " But, username is in URL: /author/peter. So, when page domain/author/peter was loaded, on that page this shortcode should be generated:

[broo_user_badges username="peter"]

I found this

add_shortcode('name', 'get_name');
function get_name() {
   return $_GET['name'];
}

here How to get URL param to shortcode? but I do not understand how to use that on my case (/author/peter url structure).

Edit: plugin /

I have this shortcode

[broo_user_badges username=""]

and I have to put username between " " But, username is in URL: /author/peter. So, when page domain/author/peter was loaded, on that page this shortcode should be generated:

[broo_user_badges username="peter"]

I found this

add_shortcode('name', 'get_name');
function get_name() {
   return $_GET['name'];
}

here How to get URL param to shortcode? but I do not understand how to use that on my case (/author/peter url structure).

Edit: plugin https://wordpress/plugins/badgearoo/

Share Improve this question edited Apr 13, 2017 at 12:37 CommunityBot 1 asked Oct 16, 2016 at 11:37 StefanStefan 211 silver badge4 bronze badges 1
  • Please edit your question mentioning the shortcode plugin you are using. – Max Yudin Commented Oct 16, 2016 at 12:23
Add a comment  | 

2 Answers 2

Reset to default 1

This should work:

function get_name() {
    $url = "http://domain/author/peter";

    if (preg_match("/author/", $url )) {
        $lastSlash = strrpos( $url, "/");
        return substr( $url, $lastSlash, strlen($url));
    }
}

This will return everything after the last slash.

Two ways to get author name from url

  1. Using get_query_var()
// http://example/author/peter
$author_name = get_query_var('author_name'); // peter
  1. Using get_queried_object()
// http://example/author/peter
$author_info = get_queried_object();
$author_name = $author_info->user_login; // peter

// print_r($author_info); // to see more information about Author

For more information read Custom Author Information

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far