{"id":384,"date":"2013-10-17T21:59:08","date_gmt":"2013-10-18T01:59:08","guid":{"rendered":"http:\/\/www.bemdesign.com\/wordpress\/?p=384"},"modified":"2013-10-17T22:11:46","modified_gmt":"2013-10-18T02:11:46","slug":"a-very-useful-wordpress-code-snippet-to-know","status":"publish","type":"post","link":"https:\/\/www.bemdesign.com\/wordpress\/2013\/10\/17\/a-very-useful-wordpress-code-snippet-to-know\/","title":{"rendered":"A very useful WordPress code snippet to know&#8230;"},"content":{"rendered":"<p>As I was working on a custom WordPress theme (based on <a href=\"http:\/\/underscores.me\/\" target=\"_blank\" title=\"The underscore starter theme from Automattic\">_s<\/a>) I found myself trying to get the slug of a post to use as a class for additional styling. Unfortunately, WordPress doesn&#8217;t have a <code>get_the_slug()<\/code> function or a <code>the_slug()<\/code> function. Well, not built out like that at any rate. The &#8220;why not?&#8221; is basically because the global <code>$post<\/code> object contains that info and thus can be found already and no extra functions needed. And then I ran into this wonderful bit of code from <a href=\"http:\/\/www.tcbarrett.com\/2013\/05\/wordpress-how-to-get-the-slug-of-your-post-or-page\/\" target=\"_blank\">TCBarret<\/a> (code commenting is mine):<\/p>\n<pre>\r\n\/*This function gets the slug of a post \r\n- you need this to use the next code snippet.\r\n*\/\r\nfunction get_the_slug( $id=null ){\r\n  if( empty($id) ):\r\n    global $post;\r\n    if( empty($post) )\r\n      return ''; \/\/ No global $post var available.\r\n    $id = $post->ID;\r\n  endif;\r\n\r\n  $slug = basename( get_permalink($id) );\r\n  return $slug;\r\n}\r\n<\/pre>\n<p>So again, the above code will get_the_slug();. Now to use it, we want a filter and echo out the filtered post ID as the_slug(). And that code is:<\/p>\n<pre>\r\n\/*This adds a filter and allows us to display the results of the\r\nget_the_slug() function noted above.\r\n*\/\r\nfunction the_slug( $id=null ){\r\n  echo apply_filters( 'the_slug', get_the_slug($id) );\r\n}\r\n<\/pre>\n<p>How to use this? Well I decided to build out a little utility plugin which incorporates the above code so I can use it pretty much in any WordPress install &#8211; it&#8217;s not tied to a theme or what not. You can use the above code in a theme&#8217;s functions.php instead of a plugin but I think a plugin is a better choice as it means the functionality is usable across whatever theme you decide to use. Anyway, special thanks to <a href=\"http:\/\/www.tcbarrett.com\" title=\"TC Barret Rocks - he provided this awesome code. Go thank him.\" target=\"_blank\">TCBarret<\/a> for his excellent (and very useful) code.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As I was working on a custom WordPress theme (based on _s) I found myself trying to get the slug of a post to use as a class for additional styling. Unfortunately, WordPress doesn&#8217;t have a get_the_slug() function or a the_slug() function. Well, not built out like that at any rate. The &#8220;why not?&#8221; is [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,4],"tags":[31,12],"class_list":["post-384","post","type-post","status-publish","format-standard","hentry","category-technology","category-work","tag-code","tag-wordpress"],"_links":{"self":[{"href":"https:\/\/www.bemdesign.com\/wordpress\/wp-json\/wp\/v2\/posts\/384","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bemdesign.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bemdesign.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bemdesign.com\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bemdesign.com\/wordpress\/wp-json\/wp\/v2\/comments?post=384"}],"version-history":[{"count":3,"href":"https:\/\/www.bemdesign.com\/wordpress\/wp-json\/wp\/v2\/posts\/384\/revisions"}],"predecessor-version":[{"id":387,"href":"https:\/\/www.bemdesign.com\/wordpress\/wp-json\/wp\/v2\/posts\/384\/revisions\/387"}],"wp:attachment":[{"href":"https:\/\/www.bemdesign.com\/wordpress\/wp-json\/wp\/v2\/media?parent=384"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bemdesign.com\/wordpress\/wp-json\/wp\/v2\/categories?post=384"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bemdesign.com\/wordpress\/wp-json\/wp\/v2\/tags?post=384"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}