WordPress.org

Ready to get started?Download WordPress

Forums

Trying to extract "postname" (6 posts)

  1. Arlo
    Member
    Posted 9 years ago #

    I'm doing the following bit of trickery to allow for a file download of the same name as the post title:

    <a href="/files/<?php the_title(); ?>.zip">Download <?php the_title(); ?></a>

    The obvious problem is that it inserts a %20 where spaces are. I would prefer to have the filename match the %postname%, that is "post-title" becomes "post-title.zip". However, there's no "the_postname", and 'the_permalink' gives me, well, the whole link. I've looked through the codex, and nothing seems to do it.
    Any ideas?

  2. ColdForged
    Member
    Posted 9 years ago #

    If this is in The Loop, you can simply go right to the variable: $post->post_name.

  3. Arlo
    Member
    Posted 9 years ago #

    It is in the loop. I'm not clear on the syntax though..I dug through the codex, but nothing seems to work (sorry, I'm partially php newb...)

    I tried variations of this: <?php $post->post_name() ?> but no go...

  4. ColdForged
    Member
    Posted 9 years ago #

    You'll need to echo it. <a href="/files/<?php echo $post->post_name;?>.zip">Download <?php echo $post->post_name; ?></a>

  5. davidchait
    Member
    Posted 9 years ago #

    You could also do it by post ID instead of name... I've done that before, works well.

    -d

  6. Arlo
    Member
    Posted 9 years ago #

    ColdForged worked like a charm, thanks!
    David -yeah, post_id works, but it doesn't make for pretty filenames :)

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.