• Hi,

    To begin with I started using the loop function to create a list of thumbnails but when I clicked on a thumbnail/link and got to the post page it only showed the thumbnail for the post. Therefore I’m using the get_posts.

    So, I’ve made a list of thumbnails which are linked to their post. The list only needs to show 6 thumbnails at the time. The thing is that I need at “Next” and “Previous” button – just like using the loop.

    My question is: Is it possible to make a “Next/Previous” function by using get_posts, or how can I make a list by using the loop function without having the problem when entering a post page?

    My get_posts code:

    <?php
    	$lastposts = get_posts('numberposts=6');
    	 foreach($lastposts as $post) :
    	setup_postdata($post);
    ?>
    
    <?php $thumbnail = get_post_meta($post->ID, 'thumbnail', $single = true); ?>  
    
    <?php if($thumbnail !== '') { ?>
    
    	<div class="item fl">
    	<a href="<?php the_permalink() ?>">
    	<img src="<?php echo $thumbnail;?>" alt="<?php the_title(); ?>" border="0" class="pic_thumb"/></a>
    
    <?php } ?>
    </div>
    <?php endforeach; ?>

    I’m a sorta new to WordPress, so please, help me out here? :o)

Viewing 15 replies - 1 through 15 (of 18 total)
  • Here is a function adapted from the rewire theme that can be called from within the Loop to display an image from a post:

    # Displays post image attachment (sizes: thumbnail, medium, full)
    function dp_attachment_image($postid=0, $size='thumbnail', $attributes='') {
    if ($postid<1) $postid = get_the_ID();
    if ($images = get_children(array(
       'post_parent' => $postid,
       'post_type' => 'attachment',
       'numberposts' => 1,
       'post_mime_type' => 'image',)))
       foreach($images as $image) {
          $attachment=wp_get_attachment_image_src($image->ID, $size);
          ?><a href="<?php the_permalink($postid); ?>" ><img src="<?php echo $attachment[0]; ?>" <?php echo $attributes; ?> /></a><?php
       }
    }
    Thread Starter tuplet

    (@tuplet)

    I use custom fields to get the thumbnail. I just link to the thumbnail. So what I need, is a list of the 6 most resent posts – and a next/prev button.
    The problem can be solved by using the loop function, but as soon as I go to a post page, the list only shows one thumbnail – the one thumbnail connected to the post ID.

    It’s quite simple but I can’t explain it right :o/

    I guess I still don’t understand. Please explain what you want to see when you click on a thumbnail. You say you only see the one thumbnail. Do you mean that you don’t see the post content?

    Thread Starter tuplet

    (@tuplet)

    What I need, is a list of six posts (ordered by date), with a next/prev function (“show next 6 posts”).

    I’ve used the loop to get this working, but when I’m clicking on one of the posts in the list and goes to the post page, the list only showes one post in the list – the one connected to the post ID.

    Dos it make sense? I’ll put it online soon so that you can see it :o)

    Thanks for your patience, mate!
    I appreciate it!

    When you click on one of the posts in the list, what do you want to see?

    You are probably using the_permalink() to set up the links. That will show only one post. You need to change from the_permalink() to something else, but I can’t tell what to change to, because I don’t know what you want to see.

    Thread Starter tuplet

    (@tuplet)

    When I click I want to see the one post content, and so it does, but on the frontpage it showes six recent posts in the list, but when I click on the link it only showes one post in the list – the one post connected to the post ID.

    I’ll try to put it online now, so that you can see it. :o)

    Thread Starter tuplet

    (@tuplet)

    Here is the link

    The design is build up in a simple one-page-only style. The content will be shown in the main videobox. When you click at one of the thumbnails, the point is that you’ll see the content of the clicked thumbnail/ID.

    If you try to click at one of the thumbnails, you will see that the list of six thumbnails will disappear, and only show the one thumbnail.

    The second problem is, that the content of the main videobox dos not chance. That’s because of the code:

    <?php
    	$lastposts = get_posts('numberposts=1');
    	foreach($lastposts as $post) :
    	setup_postdata($post);
    ?>

    I’ve tried to use the loop, and it works fine, but on the frontpage it should only show the latest post – not the last six of them.

    Hope it’s not too confusing :o)
    If it’s for any use, then here is the code for the main videobox:
    `<div id=”video_frame”>
    <?php
    $lastposts = get_posts(‘numberposts=1’);
    foreach($lastposts as $post) :
    setup_postdata($post);
    ?>

    <?php $video = get_post_meta($post->ID, ‘video’, $single = true); ?>
    <?php if($video !== ”) { ?>
    <?php echo $video;?>
    <?PHP } ?>
    </div>

    <div id=”video_dec”>
    <h3>“><?php the_title(); ?></h3>
    <?php the_excerpt(); ?>
    </div>
    <?php endforeach; ?>
    </div>`

    and here’s the code for the movieroll/thumbnail function:

    `<div class=”news_items”>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

    <?php $thumbnail = get_post_meta($post->ID, ‘thumbnail’, $single = true); ?>
    <?php if($thumbnail !== ”) { ?>

    <div class=”item fl”>
    “><img src=”<?php echo $thumbnail;?>” alt=”<?php the_title(); ?>” border=”0″ class=”pic_thumb”/>
    <?php } ?>
    </div>

    <?php endwhile; else: ?>
    <p><?php _e(‘Sorry, no posts matched your criteria.’); ?></p>
    <?php endif; ?>
    <?php posts_nav_link(‘ — ‘, __(‘« Newer Posts’), __(‘Older Posts »’)); ?>

    </div>`

    Thread Starter tuplet

    (@tuplet)

    Let me scale it down:

    Main videobox:
    – It should only have 1 post at the time!
    – The content should change depending on the post ID.
    – On the frontpage it should show the latest post.

    Videoroll:
    – Should always show the latest six posts in order.
    – A next/previous function if there is more than six posts in the list.

    I think I understand now what you want, but the solution is longer/more complex than I can detail here.

    The basic approach would involve passing a parameter in the URL to select the post to show in the Main videobox. Here is an outline of what I think is needed:

    • Add code to get the parameter and set the query for the main videobox.
    • Add a query_posts function for the Videoroll.
    • Modify the link creation for the thumbnails to add the ID of the thumbnail’s post to the URL parameter.
    • Add filters to next/previous_posts_link_attributes to add the parameter to the next/previous links. Without this parameter, these links would cause the latest post to show.

    If you want to pursue this, email me at m_a_mcdonald =at= bellsouth =dot= net, and I will try to give more details. Unfortunately, I won’t be able to test the code, so you will have to do some debugging on your own.

    OK, forget the email – I had a go at modifying the code you posted for the scheme above, and posted the it at http://wordpress.pastebin.ca/1823181.

    IT IS UNTESTED – BE SURE TO BACK UP!! You will need to integrate it back into your file.

    Thread Starter tuplet

    (@tuplet)

    If you have a solution how to make a next/prev function without using the Loop then it should be easy. Because then I use the Loop for the main videobox, and set the setting only to show one recent post (Admin > Settings > Reading) – then it’s solved.

    And for the Videoroll, something like:

    <?php
    	$listposts = get_posts('numberposts=6&order=ASC');
    	foreach($listposts as $post) :
    	setup_postdata($post);
    ?>

    And then a next/prev function?
    Add filters to next/previous_posts_link_attributes – how to you make that?

    Look at the code I posted in the pastebin – it has the filter functions at the bottom.

    OOPS – after thinking it over, I realized the filters will not work. They add ‘attributes’, not ‘parameters’ to the link. I will try to come up with an alternative.

    I also think that using the Loop for the Main videobox will not work because it will always show just the most recent post, not the one clicked in the Videoroll.

    Thread Starter tuplet

    (@tuplet)

    I’ve tried using the loop for the main videobox and it workes just fine :o)

    I think it would as long as you don’t have next/previous links for the Videoroll (VR). Here is my thinking:

    If you hover on an image in the VR, you will see that it adds ?p=22 (for example) to the URL, so the next time the page is loaded, it will display post 22.

    However, if you have next/previous links, those links would not have a means of knowing what to pass as the post for the Main area.

    It comes down to the fact that you need to pass both the post ID for the Main area, and the page number for the VR in the URL. I just thought it would be a little easier to add the post ID to the built-in next/previous functions.

Viewing 15 replies - 1 through 15 (of 18 total)

The topic ‘Get next 6 thumbnails – get_posts or loop?’ is closed to new replies.