• Resolved Alex Cragg

    (@epicalex)


    I have set up a custom loop for featured items using this page and have modified it to include an array so that more than one post can later be blocked from appearing again. This is all taking place in my index.php.

    see pastebin for example.

    I want to display the last 5 posts in the Featured category and display them using a crossfader script to rotate through them. This script needs the ids of the divs to be scrolled and I have created these ids by using <?php the_ID(); ?>, and I now need to call these in the javascript(see pastebin again). However, this script needs to be outside the loop for it to work, and <?php the_ID(); ?> isn’t working because i’m not on a single page, and so the many IDs I need cannot be ‘found’!

    So my question is this: how can I construct a variable that will record the 5 ids of the last 5 posts in the ‘Featured’ category, so that I can call them again outside the loop.

    thanks in advance

    Alex

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator Samuel Wood (Otto)

    (@otto42)

    www.remarpro.com Admin

    I thought I understood the issue, but now I’m not sure.

    You’re already copying the post ID’s into the $do_not_duplicate array. So what’s the problem? You already have the IDs in that array.

    Note that you should really be using get_the_ID() inside the Loop instead of referencing $post->ID directly.

    Also, you can replace this:
    if($post->ID == $do_not_duplicate[0] || $post->ID == $do_not_duplicate[1] || $post->ID == $do_not_duplicate[2] || $post->ID == $do_not_duplicate[3] || $post->ID == $do_not_duplicate[4] )

    with this:
    if (array_search($post->ID, $do_not_duplicate) !== false)

    Yes, that’s a bang with 2 equal signs, to check for not identity instead of not equality.

    Thread Starter Alex Cragg

    (@epicalex)

    True, I have already stored them there, but for some reason I thought that could only be used for not duplicating a post ID, didn’t realise it was just a standard variable.

    I’ve changed the calls to use the array search, but I’m having trouble calling the post IDs back from $do_not_duplicate.

    Ive tried the following:

    'cf<?php the_ID() == $do_not_duplicate[n] ; ?>'
    This returns the ID of the last item only, 3 in this case, whatever I put in as ‘n’.

    'cf<?php get_the_ID() == $do_not_duplicate[n] ; ?>'
    This returns a blank space.

    'cf<?php $post->ID == $do_not_duplicate[n] ; ?>'
    This also returns blank.

    Are there any further pointers you can give me, and thanks again for the help!

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.remarpro.com Admin

    The correct answer would be along these lines:

    <script type="text/javascript">
    var cf = new Crossfader( new Array(
    <?php
    foreach ($do_not_duplicate as $id) {
    $stringlist[] = "'cf".$id."'";
    }
    echo implode (',',$stringlist);
    ?>
    ), 1000, 4000 );
    </script>

    The way that works is that it builds the ‘cf#’ strings, then implodes them (puts them together using the commas in between), then echos that to the page, and continues with the script.

    Thread Starter Alex Cragg

    (@epicalex)

    Wow! It works perfectly, thanks so much, there is no way I would have ever come across that, as you probably guessed, my php knowledge isn’t great, I just try and follow examples from various places!

    Thanks again!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘the_ID outside of loop’ is closed to new replies.