This question already has an answer here:

I'm just starting to create new theme and i'm new to code. I'm stuck in customizing the post thumbnail code, i tried add

<?php previous_post_link(get_the_post_thumbnail(get_previous_post(), array(
                        'class' =>  'rounded-lg object-cover'
           )) . '<h4 class="text-center">%link</h4>',

but it doesn't seem to work. did my array mistakenly placed in the code?

marked as duplicate by Jacob Peattie, bosco, nmr, Nathan Johnson, fuxia Aug 9 at 15:46

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Are you saying it's not working because you can't see the CSS effect you expected to see, or are you saying it's not working because the class attribute does not have those classes when you inspect the HTML? – Jacob Peattie Jul 16 at 1:06
  • 2
    2nd argument is thumbnail size (usually 'post-thumbnail') so if you add that the attributes will shift to their correct (3rd) argument position and should be all good. developer.wordpress.org/reference/functions/… – majick Jul 16 at 1:12
  • guys sorry, i think i already fix it, and now i have problem with customizing image width and height, here are my latest code. – haiz85 Jul 16 at 1:32

Sorry for late reply, already fixed, i miss understood about array, so here the code that completely run as i want it.

 $next_post = get_next_post();
     get_the_post_thumbnail($next_post->ID, 'prev_next_img', array(
               'class' => 'rounded-lg object-fill w-full max-h-full' 
     )) . '<h4 class="text-center mt-2 lg:text-base text-sm no-underline text-black leading-snug font-medium">%title</h4>',                                                

in function.php

i put custome image size

add_image_size('prev_next_img', 370, 270, true);

i think i already fix it, and now i have problem with customizing image width and height, here are my latest code.

 $prev_post = get_previous_post();
 get_the_post_thumbnail($prev_post->ID, 'custom-size-image', array(
         'class'     =>  'rounded-lg object-cover',
         'sizes'     =>  'width="371" height="270"'
 )) . '<h4 class="text-center">%link</h4>',

but 'sizes' part doesn't seem to work, please help

  • 3
    The 3rd argument is for HTML attributes, so you should be setting 'width' => 371 and 'height' => 270 separately, not sizes. – Jacob Peattie Jul 16 at 1:40
  • Ok, thanks for the info, i'll let you know if it work – haiz85 Jul 16 at 2:35
  • I just tried what your suggested , but return "parse error: syntax error, unexpected "height" , do i need to put comma after the number? – haiz85 Jul 16 at 2:41
  • Yes. These are values in a PHP array. – Jacob Peattie Jul 16 at 2:42
  • Already add it, still error, parse error:syntax error, u expected ',' – haiz85 Jul 16 at 2:48

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