data attribute isn’t outputting anything

For some reason the data-mainsrc attribute isn’t outputting anything. I’m trying to get it to output the image url.

<?php $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'home-thumb' );$url = $thumb['0']; ?>

Read More

Did I format the code incorrectly?

Full code

    <!-- Start the loop -->
    <?php $home_query = new WP_Query('post_type=projects');

    while($home_query->have_posts()) : $home_query->the_post(); ?>

    <article class="project">
        <img width="375" height="375" src="<?php bloginfo( 'template_url' ); ?>/img/loading.gif" data-mainsrc="<?php $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'home-thumb' );$url = $thumb['0']; ?>" class="attachment-home-thumb" alt="<?php the_title(); ?>">
            <div class="overlay">
                <a class="post-link expand" href="#" rel="<?php the_ID(); ?>">+</a>
            </div>
    </article>

    <?php endwhile; ?>
    <?php wp_reset_postdata(); // reset the query ?>

</div><!-- #projects-list -->

Related posts

Leave a Reply

1 comment

  1. you are not echoing anything, just storing in a variable, so this

    data-mainsrc="<?php
    $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'home-thumb' );
    $url = $thumb['0']; ?>"
    

    probably ends up looking like this once it is evaluated by php

    data-mainsrc=""

    The functions that start with get_… only return a value, unlike the functions that start with the_… which echo it.

    so how about this

    <?php
    $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'home-thumb');
    $url = $thumb['0']; 
    ?>
    
    <img width="375" height="375" src="<?php bloginfo( 'template_url' ); ?>/img/loading.gif" data-mainsrc="<?php
     echo $url; ?>" class="attachment-home-thumb" alt="<?php the_title(); ?>">