1

I want to capture url slug and title from sidebar.php. For example: if url is: www.example.com/video-tuts/abc for a page which title is: awesome explanation of abc

Now I want to get the portion of the url: video-tuts and title: awesome explanation of abc from sidebar.php. Any idea?

Ryan Leach
  • 4,262
  • 5
  • 34
  • 71
Abdus Sattar Bhuiyan
  • 3,016
  • 4
  • 38
  • 72
  • Wouldn't the slug be abc, not video-tuts? – Ryan Leach May 29 '18 at 02:31
  • I want to get parameter immediate after the main domain, I knew this is slug. if video-tuts is not slug, what is it? – Abdus Sattar Bhuiyan May 29 '18 at 02:37
  • In the Stack Over flow url of https://stackoverflow.com/questions/4230846/what-is-the-etymology-of-slug the `slug` is `what-is-the-etymology-of-slug` it has no purpose, except for making the url more readable for humans. Anything that has an actual purpose, other then increasing readability, is just a portion of the url path. – Ryan Leach May 29 '18 at 02:45

1 Answers1

0

You can use $_SERVER['PATH_INFO'] to get the URL path information in PHP and then explode() that into an array to get the different parts.

However, if you just want to get the title of the post/page that is being viewed you can call global $post; in sidebar.php and then $title = get_the_title($post->ID); or $post->post_title; to get the title.

Ty Bailey
  • 2,392
  • 11
  • 46
  • 79
  • I think the Op may want the category? But I'm not 100% sure that's what it's called as my wordpress is a little rusty. – Ryan Leach May 30 '18 at 03:51