• 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 15 replies - 1 through 15 (of 17 total)
  • What about this approach:

    • Save the current post
    • Get the category and ID of the current post
    • Get all posts in that category
    • Loop through the array of posts checking IDs against the saved ID
    • Until you find the saved ID copy posts to the ‘before’ array
    • After you find the saved ID, copy to the ‘after’ array
    Thread Starter alexvaxx

    (@alexvaxx)

    Thank you for that, I’ll try it out and get back to you!

    Thread Starter alexvaxx

    (@alexvaxx)

    So, I couldn’t quite figure out how to do the whole “copy posts to array”-deal, so I endedup doing this instead:

    LINK TO PASTEBIN

    Or in other words:

    • Display the first loop and save the id in a variable
    • Create a new loop that checks against the variable to make sure it only displays the following posts
    • Create a third loop that checks against the variable to make sure it only displays the items that are “younger” than the original post

    This seems to me to be a bit ineffecient, very php and database request-heavy, so if there’s a smarter way, please let me know!

    Sorry for the confusion. If you do a query to get all posts in the category, they are in an array in $wp_query->posts. You can treat this just like any other array.

    Thread Starter alexvaxx

    (@alexvaxx)

    So the thing I don’t understand is how I store those posts that are either too “young” or too “old” to be displayed yet, in another array.

    Can I just go:

    if ( $original_post >= $post->ID ) $before[] = $wp_query->posts;

    ??

    And even if that’s the case, how do I then retrieve those posts from the before-array, when I’m ready to use them?

    I’m sorry, but I’ve read the Codex entrys on the loop so many times, but it’s still very complicated to me.

    //A

    I think this will work (UNTESTED). Assume current post ID is in $current_ID:

    $found_current = 0;
    foreach ($wp_query->posts as $apost) {
       if ($apost->ID == $current_ID) {
          $found_current = 1;
       } elseif ($found_current) {
          $after[] = $apost;
       } else {
          $before[] = $apost;
       }
    }
    Thread Starter alexvaxx

    (@alexvaxx)

    Thank you, but still: I don’t understand how I can then go ahead and actually retrieve/display those posts in the wanted order?

    Is this better?

    query_posts( array ('category_name' => $categoryname, 'posts_per_page' =>-1));
    if ( have_posts() ) : while ( have_posts() ) : the_post();
       $found_current = 0;
       if ( $original_post == $post->ID ) {
          $found_current = 1;
       } elseif ($found_current) {
          // do after stuff
       } else {
          //do before stuff
       }
    endwhile; endif;
    Thread Starter alexvaxx

    (@alexvaxx)

    Nope ??

    It just loops through all the posts in a regular order starting at number 1.

    And what I don’t understand is, you say
    elseif ($found_current)
    but “if found_current” what? I guess its just a syntax I’m unfamiliar with, but it seems to me you’re not comparing it against anything?

    But thank you anyway.

    Did you replace lines 9 – 19 in your pastebin code with the lines I gave you? The value of $original_post is set in line 5, so that first loop must remain.

    The if statement translated: if ($found_current has a value other than zero)

    Thread Starter alexvaxx

    (@alexvaxx)

    I tried that now, and it doesn’t quite work. Assume I’m viewing post no. 5, it ends up showing first post no 5 (yay), but then it shows 1,2,3,4, and 6,7,8,9,10 etc.
    But what I want is: 5, 6, 7, 8, 9, 10 and then, 1, 2, 3, 4.
    Oh well.

    Just reverse the posts array:

    query_posts( array ('category_name' => $categoryname, 'posts_per_page' =>-1));
    if ( have_posts() ) :
       $wp_query->posts = array_reverse($wp_query->posts);
       while ( have_posts() ) : the_post();
          // rest of the code

    EDIT: That won’t quite give you what you want. I will work on a solution and get back to you.

    Thread Starter alexvaxx

    (@alexvaxx)

    Hah, now it shows 5, 10, 9, 8, 7, 6, 4, 3, 2, 1. What a mess.

    But thanks fot not losing faith ??

    Please give this a try:

    query_posts( array ('category_name' => $categoryname, 'posts_per_page' =>-1));
    if ( have_posts() ) :
       $before = array();
       $after = array();
       $found_current = 0;
       for ($i=0;$i < sizeof($wp_query->posts) ; ++$i ) {
          $this_post = $wp_query->posts[$i];
          if ($original_post == $this_post->ID) {
             $save_current = $this_post;
             $found_current = 1;
          } elseif ($found_current) {
             $after[] = $this_post;
          } else {
             $before[] = $this_post;
          }
       }
       $before[] = $save_current;
       $wp_query->posts = array_merge(array_reverse($before), array_reverse($after));
       while ( have_posts() ) : the_post();
          if ($post->ID == $original_post) continue;
    Thread Starter alexvaxx

    (@alexvaxx)

    It works! Sort of.. I took out the array_reverse function and just merged the arrays like this
    array_merge($after, $before);
    And that did the trick, the posts show up like this now: 5, 6, 7, 8, 9, 10, 1, 2, 3, 4. Fantastic. Thank you very much. But tell me:
    1. Is your version more efficient than the one I posted in the pastebin?
    2. Only if you have the time, could you explain to me what lines 3-15 in your code exactly do, just so that I feel like I’ve learned something ??

    //A.

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