Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

So I had asked (in retrospect a pretty non-question) at A little help/cleanup from a real coder? and one of the comments was to not use query_posts and instead use WP_Query. So I did a little digging and tried to implement that, now the code isn't working fully. The MultiPostThumbnails isn't pulling the img I'm only getting the the_post_thubnail not additional img. What am I doing wrong here?

function efs_get_slider(){

    $slider= '<div id="ctslider">
                <div class="flexslider">
                    <ul class="slides">';

    $my_posts = new WP_Query();
    $efs_query= "post_type=slider-image";
    $my_posts->query($efs_query);


    if($my_posts->have_posts()) : while ($my_posts->have_posts()) : $my_posts->the_post(); 

        $flexID = $my_posts->ID;
        $img1= wp_get_attachment_image( get_post_thumbnail_id($flexID),'full',0 );
        $imagemain =  preg_replace( '/(width|height)=\"\d*\"\s/', "", $img1 );

        if (class_exists('MultiPostThumbnails')) {
            $image_name = 'feature-image-2';
            if (MultiPostThumbnails::has_post_thumbnail('slider-image', $image_name)) {
                $image_id = MultiPostThumbnails::get_post_thumbnail_id(
                    'slider-image',
                    $image_name,
                    $flexID
                );
                $attr = array('class' => "secondaryslider");
                $image = wp_get_attachment_image( $image_id, 'full', false, $attr );
                $imagesecondary = preg_replace(
                    '/(width|height)=\"\d*\"\s/',
                    "",
                    $image
                );
            }
        };

        $cttitle = get_the_title();
        $ctexcerpt = get_the_excerpt();

        $slider.='<li><div class="fourcol secondaryimg">'.$imagesecondary.'</div>';
        $slider.='<div class="threecol flex-caption"><h3>'.
            $cttitle.'</h3><p>'.$ctexcerpt.'</p></div>';
        $slider.='<div class="fivecol last">'.$imagemain.'</div></li>';


    endwhile; endif; wp_reset_postdata();


    $slider.= '</ul>
    </div></div>';

    return $slider;
}
share|improve this question
    
What point is it failing at? Does it not get $image_id? Or is it after that? –  m0r7if3r Apr 21 '12 at 2:26
    
yes, it's not getting the $image_id...it's like it's not passing the post ID to the MultiPostThumbnails::get_post_thumbnail_id –  Brian Thornton Apr 21 '12 at 6:24
    
does get_the_ID() work if you try to execute it? –  m0r7if3r Apr 21 '12 at 8:46
add comment

1 Answer

up vote 1 down vote accepted

I'm not familiar with MultiPostThumbnails, but I suspect your problem is:

$flexID = $my_posts->ID;

That code appears to be trying to get the ID of the WP_Query object itself. I don't think that exists. Even if it does, it won't do you much good.

Rather, use this:

$flexID = get_the_ID();

That code gets the ID of the current post in the loop.

In general, make sure to review the WP_Query codex page. You can tighten things up quite a bit. For instance, your whole query can be on one line:

$my_posts = new WP_Query( 'post_type=slider-image' );
share|improve this answer
    
+1 for the last line. I think the underlying problem is that the new WP_Query() is instantiated without the custom arguments. To use custom args, the construct should be $my_query = new WP_Query( $args ). –  Chip Bennett Apr 21 '12 at 22:20
    
mrwweb, that did the trick...AND it explained what I was missing. my_posts->id getting the id of the wp_query...oi. And, cleaned up a few lines. @m0r7if3r looks like you were on the right track. Now I'm going to read up on get_the_ID() vs $post->ID or whatever the hell I was trying to do! And, Chip, I was able to get it to work before I cleaned up the custom args, so it did work by instantiating w/o the custom args, but...cleaner code all on one line. Thanks!! –  Brian Thornton Apr 21 '12 at 23:34
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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