0

I'm using the code below to query all other posts with same value in custom field FeaturedArtists. Works great except the images. All images are the same as the current post. If I change catch_that_image into the_post_thumbnail the same happens.

            <?php $FeaturedArtist = get_post_meta($post->ID, 'FeaturedArtist', true); if($FeaturedArtist): ?>
        <?php $chapters = get_posts('numberposts=-1&meta_key=FeaturedArtist&orderby=date&order=ASC&meta_value='.$FeaturedArtist); foreach($chapters as $chapter):?>
            <div class="box more">
            <?php if($chapter->ID != $post->ID): ?>
            <a href ="<?php echo get_permalink($chapter->ID); ?>" alt="CONTEST ENTRY by <?php the_title(); ?>" title="CONTEST ENTRY by <?php the_title(); ?> ">
                <img src="<?php echo catch_that_image() ?>" width="625px" alt="<?php the_title(); ?>"/>
                <div class="articleTitle">
                    <span class="titleType">CONTEST ENTRY<br /><span class="titleArticle"><?php the_title(); ?></span></span>
                </div>
            </a>
            <?php endif; ?>                  
            </div>
        <?php endforeach; ?>
        <?php endif; ?>

Anyone an idea on how to fix this? Thanks for helping me out!

1 Answer 1

1

It's probably because catch_that_image() is receiving the global $post_id the same with the_post_thumbnail() in your code you could pass the id of your current chapter catch_that_image($chapter->ID) or the_post_thumbnail( $chapter->ID ) this should solve your problem or you can try this code, it should work.

<?php
global $post; 
$current_post = get_the_ID();

$FeaturedArtist = get_post_meta($current_post, 'FeaturedArtist', true); 
if($FeaturedArtist):
    $chapters_args = array(
        'posts_per_page' => -1,
        'meta_key'       => 'FeaturedArtist',
        'meta_value'     => $FeaturedArtist,
        'orderby'        => 'date',
        'post__not_in'   => array( $current_post )
    ); 
    $chapters = get_posts( $chapters_args ); 

    foreach( $chapters as $post ):
        setup_postdata( $post );
        ?>
        <div class="box more">
            <a href="<?php echo esc_url( get_permalink() ); ?>" alt="CONTEST ENTRY by <?php echo esc_attr( get_the_title() ); ?>" title="CONTEST ENTRY by <?php echo esc_attr(get_the_title()); ?> ">
                <img src="<?php echo catch_that_image() ?>" width="625px" alt="<?php echo esc_attr( get_the_title()); ?>"/>
                <div class="articleTitle">
                    <span class="titleType">CONTEST ENTRY<br /><span class="titleArticle"><?php the_title(); ?></span></span>
                </div>
            </a>              
        </div>
    <?php 
    endforeach; 
    wp_reset_postdata();
endif; 
?>

What happens here is I just separeted your code to be a little bit more readable and in the foreach loop I've used the setup_postdata() that is used to set the global post data and with this you can use the template tags without having to pass the ID for the functions, you can use get_the_permalink() instead of get_the_permalink($chapter->ID) and after the foreach loop I just "cleaned" the post data with the original post data for this page using wp_reset_postdata()

1
  • Works like a charm! Thanks Marcos. I tried to use catch_that_image($chapter->ID) or the_post_thumbnail( $chapter->ID ) but never thought of the global ID in the function codes of both.
    – AKNL
    Commented Nov 5, 2014 at 7:15

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.