0
votes

I am working on a site built on Wordpress and the single post template calls the featured image using the below line of php.

<?php $img = array('class' => 'enigma_img_responsive', 'alt' => 'the alt', 'title' => 'the title' );
    if(has_post_thumbnail()): 
    the_post_thumbnail('home_post_thumb',$img);
endif; ?>

I am trying to get this code to pull the alt and title from the media library dynamically for all featured images. From what I have seen you can't use other php inside of the array where I could list the attributes and there values. I've tried the following as a potential solution but it doesn't work.

    <?php $img = array('class' => 'enigma_img_responsive', 'alt' => 'featured imag for the_title();', 'title' => 'the_title();' );
    if(has_post_thumbnail()): 
    the_post_thumbnail('home_post_thumb',$img);
endif; ?>

The output for the above code is having "the_title();" actually show up in the alt and title attributes.

Is there a workaround for this, can't seem to find one. I assume there is since WordPress is so popular.

1
What if you remove simple quote ? Like this : 'title' => the_title(); or use get_the_title() instead of the_title()Vincent G
Using the_title() without the single quotes resulted in the title being printed outside of the image tag. However when I used get_the_title() it worked like a charm. Thanks Vincent!Tim Fell

1 Answers

0
votes

The solution, given by Vincent G. in a comment, is to swapthe_title() for get_the_title().