WordPress.org

Ready to get started?Download WordPress

Forums

Add the_slug() function please (8 posts)

  1. Hinjiriyo
    Member
    Posted 2 years ago #

    A function the_slug() would be very nice.

  2. s_ha_dum
    Member
    Posted 2 years ago #

    echo $post->post_name inside a loop.

  3. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    depending on your specific ideas about what the function is supposed to do, the function might already exist under a different name:

    http://codex.wordpress.org/Function_Reference/sanitize_title_with_dashes

  4. s_ha_dum
    Member
    Posted 2 years ago #

    Technically, what I posted isn't a function but if all you need is the slug a function is unnecessary overhead.

    Functions like the_title() are justified because of the associated filters, mostly. They do more than just echo the post title. I don't know what kind of filters or other processing you might need to run on a post slug. Most anything you do to it would break its functionality-- serving as the unique part of a post's URL.

    Just thinking out loud... if you've got an argument let's hear it.

  5. Hinjiriyo
    Member
    Posted 2 years ago #

    s_ha_dum, it is a surprising experience to google for "wordpress the_slug" and to find many, many articles and forum discussions about how to get the current slug.

    You are right with your solution. It is just that and maybe adding "global $post;" before that line into the function.

    But many theme developers seem to seek for such an easy-to-understand function, maybe for such an intuitive function name. Despite of the simplicity of the single code line you mentioned. I think there is a demand for such a simple solution - not technically, but "human beingly" ;-)

    I do not think implementing a simple code like the_slug() in the wp core is overhead. It is so easy to understand and use template tags and not to digg in objects like $post.

  6. s_ha_dum
    Member
    Posted 2 years ago #

    s_ha_dum, it is a surprising experience to google for "wordpress the_slug" and to find many, many articles and forum discussions about how to get the current slug.

    I agree. It is surprising since var_dump(get_defined_vars()); in a loop will tell you the answer.

    I do not think implementing a simple code like the_slug() in the wp core is overhead.

    Of course it's overhead. Any call to a function is overhead and when you don't need a function it is unnecessary overhead. In this case, not much overhead but it is still unnecessary.

    It is so easy to understand and use template tags and not to digg in objects like $post.

    Some things are easy. Other things are better. Besides, accessing an object shouldn't be any more confusing that accessing a variable or an array. That is basic PHP stuff.

  7. Hinjiriyo
    Member
    Posted 2 years ago #

    You are so right in a technical and programming point of view. I think a function like the_slug() would accommodate WP beginners with an intuitive and easy-to-understand way to get just the post slug in the common way of template tags.

    Of course var_dump(get_defined_vars()); gives the answer. It gives many more answers when just searching for the slug. And the answer is hidden in post_name, not in post_slug or similar.

    The many articles about how to get the slug are impressive, even for you, as you agreed. It shows that there is a demand for it. the_slug() is a gentle and comfortable way to get the slug an keeps learning curve low.

  8. s_ha_dum
    Member
    Posted 2 years ago #

    If it means that much to you, you can submit a patch. It would be a trivial function.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags