• Hi there

    I have a problem that I can’t seem to solve on my own:
    I’m doing this portfolio for a photographer friend, and we’re uploading each picture as a new blog entry, attaching it using the “featured image”-feature.
    That’s not the problem, works fine.

    The thing is I want to be able to cycle through all the pictures, jcarousel-wise, in each category. Right now when viewing a single picture, I load it through the single-post-template-file, and then using next_post_link() and previous_post_link(), you can go to the next or previous picture. That also work fine.

    But I figure, if I want to able to loop through the pictures with jcarousel, I need to write some changes to the loop in the single-post-template.

    I need to load:
    1. the current post (like now; post no. 5 for instance in a category)
    2. all the next posts in the category (no. 5 to 10 for instance)
    3. all the posts before the current post (no. 1 to 4 for instance)

    I don’t suspect this to be too complicated, I just can’t seem to wrap my head around it on my own.
    Thank you very much to everyone with ideas to throw in.

    //A.

Viewing 2 replies - 16 through 17 (of 17 total)
  • This version will be somewhat more efficient because it eliminates one call to query_posts().

    Code explanation:

    for ($i=0;$i < sizeof($wp_query->posts) ; ++$i ) {
    Loop through the posta array with an index $i.

    $this_post = $wp_query->posts[$i];
    Set $this_post to the post at index $i to save further indexing.

    if ($original_post == $this_post->ID) {
             $save_current = $this_post;
             $found_current = 1;

    If the ID of $this_post is the original ID, save the post and set $found_current so we know we found it.

    } elseif ($found_current) {
         $after[] = $this_post;
    } else {
         $before[] = $this_post;
    }

    If it is not the original ID and we have found the original, add $this_post to the after array. Otherwise, add it to the before array.

    }
    $before[] = $save_current;

    End the for loop and add the current post to the before array.

    $wp_query->posts = array_merge(array_reverse($before), array_reverse($after));
    Combine the before and after arrays back into the original posts array.

    Thread Starter alexvaxx

    (@alexvaxx)

    Thank you very much for that. You’ve been very helpful. //A.

Viewing 2 replies - 16 through 17 (of 17 total)
  • The topic ‘Custom loop in single post template’ is closed to new replies.