• Greetings, is there a way to retrive the specific ID of the current or selected post? I am trying to setup a system that will generate a unique banner for each post (don’t worry, there will only be about 30 posts total). Instead of using is_single() and specify each ID by hand, I would rather set a variable. Something like:

    $banner_name = "insert function to retrieve post ID"

    …and then later use the variable to create the filename for the image, like so:

    <img src=".../images/filename_<?php echo $banner_name; ?>

    The trick is, I don’t know the function. Plus I need this to work OUTside the Loop.

    I have looked at:
    <?php $wpdb->query('query'); ?> and query_posts()and quite figure them out, or if they are even relevant.

    Thanks in advance.

Viewing 4 replies - 1 through 4 (of 4 total)
  • way too much work

    <?php the_ID(); ?> will return the current post_id outside of the loop in single post view.

    Thread Starter jmharris903

    (@jmharris903)

    Thanks for the advice, but I am still having trouble. I can’t seem to get the_ID to work outside of the loop, even in a single post view. In the loop it works fine. Bear with me, I am relatively new to PHP.

    in the loop <p>test <?php the_ID(); ?></p> yields “test 6”, out of the loop it yields “test “.

    Again, I basically want to generate a filename based on the post ID, like so

    <img src="../images/filename_<?php the_ID(); ?>.jpg"> but I need to do this outside of the loop. It is for the header image, so it may have to load before the actual post? (not sure if that matters)

    thanks

    I know what you want to do, you dont need to re-explain it.

    What I suggested works as long as its put after the loop, (but doesn’t work when put before the loop), since the post id isn’t known at that time.

    Just to show, https://bigbox.village-idiot.org/archives/11

    that 11 is after the loop.

    I know that doesn’t solve what you are wanting to do, I just wanted to illustrate that it does work ouside the loop, it just has to be after it also.

    As far as your original question goes, I will have to ponder it some more, it’s tricker than I first thought. perhaps someone else like otto will come along with a solution in my absence.

    Thread Starter jmharris903

    (@jmharris903)

    Now I understand, thanks so much. I see what you mean about it working outside the loop, after the loop has run. Very helpful, inspiring even. Playing around with it, I decided to try to run the loop twice. The first time through, just so the_ID would work, then a second time for the actual content. Inbetween the two, is the code that generates the banner (I split header.php into 2 files with the header/banner being handled by the second). I know it is a hack (and might have serious performance issues in the future), but so far, it seems to be working.

    Just to share, another solution I am considering is <?php the_title(); ?> to generate the image filename. This seems to work both outside and before the loop (I think). The only problem with this method might be spaces, caps, or odd characters in the titles.

    Thanks again, I really appreciate it.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How to: determine current or selected post ID’ is closed to new replies.