Revision 414 is a pre-publication revision. (Viewing current revision instead.)

Get post slug function

Taken from: http://www.tcbarrett.com/2011/09/wordpress-the_slug-get-post-slug-function/ Using basename() and get_permalink() to retrieve post slug [php]</pre> <?php echo( basename(get_permalink()) ); ?> <pre>[/php] Using global $post object to retrieve post slug [php]</pre> <?php global $post; echo $post->post_name; ?> <pre>[/php]   Fully hookable function to retrieve the post slug [php] function the_slug($echo=true){ $slug = basename(get_permalink()); do_action('before_slug', $slug); $slug = apply_filters('slug_filter', $slug); if( $echo ) echo $slug; do_action('after_slug', $slug); return $slug; } [/php] This provides you with a simple way retrieve and display the post slug. I find this useful for applying unique CSS ids and classes to my HTML mark up. [html] <div id="some-post-container-<?php the_slug();?>"> Lorem whatsit here </div> [/html]

Revisions

  • April 19, 2013 @ 16:05:38 [Current Revision] by PeterLugg
  • April 19, 2013 @ 16:02:35 by PeterLugg

Revision Differences

April 19, 2013 @ 16:02:35Current Revision
Content
 Added: Taken from: <a href="http:// www.tcbarrett.com/2011/09/ wordpress-the_ slug-get-post- slug-function/" target="_blank" >http://www.tcbarrett.com/ 2011/09/wordpress-the_slug- get-post-slug- function/</a>
 Added: Using basename() and get_permalink() to retrieve post slug
 Added: [php]&lt;/pre&gt;
 Added: &lt;?php echo( basename(get_ permalink()) ); ?&gt;
 Added: &lt;pre&gt;[/php]
 Added: Using global $post object to retrieve post slug
 Added: [php]&lt;/pre&gt;
 Added: &lt;?php global $post; echo $post-&gt;post_name; ?&gt;
 Added: &lt;pre&gt;[/php]
 Added: &nbsp;
 Added: Fully hookable function to retrieve the post slug
 Added: [php]
 Added: function the_slug($echo=true){
 Added: $slug = basename(get_ permalink());
 Added: do_action('before_slug', $slug);
 Added: $slug = apply_filters( 'slug_filter', $slug);
 Added: if( $echo ) echo $slug;
 Added: do_action('after_slug', $slug);
 Added: return $slug;
Deleted: Added: }
 Added: [/php]
 Added: This provides you with a simple way retrieve and display the post slug. I find this useful for applying unique CSS ids and classes to my HTML mark up.
 Added: [html]
 Added: &lt;div id=&quot;some- post-container-&lt;?php the_slug();?&gt;&quot;&gt;
 Added: Lorem whatsit here
 Added: &lt;/div&gt;
 Added: [/html]

Note: Spaces may be added to comparison text to allow better line wrapping.

No comments yet.

Leave a Reply