0

I have been working on using featured image to create a list of items on the right column here:

http://www.julianamaeberger.com/soma/news/

The current featured image is now being given a class of "highlighted" so that you can tell which image is related to the post. I now need to move that li class="highlighted" to the top of the list so that it's always visible.

Is jquery sortable the best option here?

Thanks for any help in the right direction.

1 Answer 1

0

Can't you do this in HTML? So instead of printing everything in your loop, append it to a string that you print in the end. That way you can keep special stuff apart.

I assume this is a follow-up question to your previous question about featured images, so I will use the code from my answer there as an example.

<?php
$testimonials = get_posts( array(
    'category_name' => 'testimonial',
    'numberposts' => -1,
    'order' => 'DESC'
) );
$highlighted_testimonial_thumbnail = '';
$other_testimonial_thumbnails = '';
foreach ( $testimonials as $testimonial ) {
    if ( $testimonial->ID == $current_testimonial_id ) {
        $highlighted_testimonial_thumbnail = '<li class="highlighted">' . get_the_post_thumbnail( $testimonial->ID, 'nav' ) . '</li>';
    } else {
        $other_testimonial_thumbnails .= '<li>' . get_the_post_thumbnail( $testimonial->ID, 'nav' ) . '</li>';
    }
}
echo '<ul class="portfolio">';
echo $highlighted_testimonial_thumbnail;
echo $other_testimonial_thumbnails;
echo '</ul>';

Or more generic: instead of:

foreach ( $array_of_stuff as $stuff ) {
    echo $stuff
}

Do it like this:

$output = '';
$featured_output = '';
foreach ( $array_of_stuff as $stuff ) {
    if ( is_featured( $stuff ) ) {
        $featured_output = $stuff;
    } else {
        $output .= $stuff;
    }
}

echo $featured_output;
echo $output;

Almost all functions in WordPress that echo something have an equivalent that doesn't echo but just returns it. get_the_content() vs the_content() for example.

3
  • I had a feeling I was looking at this from the wrong angle. This worked perfected! Commented Jan 2, 2011 at 17:09
  • I did notice you added to your response - I had spent a day watching intro PHP tutorials to try to understand the first response, which turned out to be a good idea, in any case. This morning I saw that you edited your response and that was super helpful. I'm curious if you can recommend any resources on PHP and Wordpress development. I was maybe going to post this as an additional question, but I'm not sure if it's appropriate for this forum. It said it might be flagged as subjective. Commented Jan 2, 2011 at 17:17
  • @user1858: I could find at least two questions on this site about training material: one about books and one about generic material. I hope this helps you. And welcome to this site! I hope to see more good questions and answers from do. Do you have a username to use instead of the generic user1858?
    – Jan Fabry
    Commented Jan 2, 2011 at 18:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.