• This is the_Loop that i’m using to display some post links:

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <li><a href="<?php echo get_permalink() ?>"><?php echo $post->post_title; ?></a></li>
    <?php endwhile; ?>
    <?php else : ?>
    &nbsp;
    <?php endif; ?>

    I would like to save the_ID of the first post in the loop in a variable that I could use later.

    Something like
    <?php $myVar = the_ID(); ?>

    But I can’t put that within the loop because firstly, it will print on the page, secondly it will be overwritten in every “loop” and thirdly, it seems to be emptied at the end of the loop.

    What can i do? Please, any help will be hugely appreciated!

Viewing 7 replies - 1 through 7 (of 7 total)
  • Just before the loop, put this:
    <?php $FirstPost = True; ?>

    Then inside the loop, put this:
    <?php if ($FirstPost == True) {
    $myVar = $post->PostID;
    $FirstPost = True;
    } ?>

    The first line sets up a varible that will hold a true/false flag. The second code set checks that flag, if it’s set, then it sets your variable, and then turns off the flag.

    -tg

    Thread Starter fredrik

    (@fredrik)

    That is so simple and clever at the same time! However, the $myVarvariable is always empty (false?) for me. It never returns a post ID number.

    What is the $FirstPost variable, is that a built in WP one or do I need to do something else?

    But thank you anyway, really appreciate it!

    No, it’s not built into WP…. you have to create it…. if it’s not working then try this:
    Change <?php $FirstPost = True; ?>
    to this
    <?php var$FirstPost;
    $FirstPost = True; ?>

    -tg

    That code example looks a bit wacked.

    The following code would get the last PostID but $post->PostID is not defined – hence the false value you were getting.

    <?php if ($FirstPost == True) {
    $myVar = $post->PostID;
    $FirstPost = True;
    } ?>

    The corrected code is I believe:

    <?php if ($FirstPost == True) {
    $myVar = $post->ID;
    /* Now set to false as we have collected an ID */
    $FirstPost = False;
    } ?>

    You should not need to change the first line from:

    <?php $FirstPost = True; ?>

    For this to work.

    Thread Starter fredrik

    (@fredrik)

    Man I love this place sometimes! That works great. Thanks guys! The variable returns the right value now.

    Now, I suck at php and I can’t put that variable into my next query. This is what I want to do:

    <?php $my_query = new WP_Query('p=$myVar&showposts=1'); ?> but I guess I need to break up the php line somehow right?

    Thread Starter fredrik

    (@fredrik)

    Damn, I almost forgot. In addition to the above I was wondering if there is a similar way to extract the category ID from the loop to use i my second loop as well?

    The reason why your query doesn’t work is because variables are not expanded in single quoted strings (see this page for more info on php strings and quoting – http://uk.php.net/types.string )

    Try this instead:

    <?php $my_query = new WP_Query("p=$myVar&showposts=1"); ?>

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Save variable from The_Loop??!’ is closed to new replies.