0

I want to use an image size on a Custom Post Type (which has featured image enabled).

in functions.php I added:

add_theme_support( 'post-thumbnails' );
add_image_size( 'my-non-working-size', 600, 300, true );

The custom post type is called 'homeslide' as I use it to build a a carousel slider on home. To get all my slides I do :

$args = array(
    'post_type'   => 'homeslide'
);
$mypages = get_posts( $args );

I loop through the results and for every slide, I want to output the featured image in my custom size. This is my attempt:

the_post_thumbnail($page->ID, "my-non-working-size");

This doesn't work. It does output the featured images, but they are not custom size, and they aren't cropped. Yet the same approach works within the theme's while( have_posts() ) loop.

What am I doing wrong?

EDIT: I was wrong about it outputting the featured images. It actually doesn't do anything. Although has_post_thumbnail( $page->ID ) returns true for every slide.

EDIT 3:

My whole code used for trying to display the featured images:

<?php
    $args = array(
        'post_type'   => 'homeslide'
    );
    $mypages = get_posts( $args );
    ?>
    <div class="your-class">
        <?php
        foreach( $mypages as $page ) {
            ?><div>
            <?php

            $content = $page->post_content;
            if ( ! $content ) // Check for empty page
                continue;

            ?>
            <h2><a href="<?php echo get_page_link( $page->ID ); ?>"><?php echo $page->post_title; ?></a></h2>
            <div class="entry"><?php
                if ( has_post_thumbnail( $page->ID )  ) {?>
                    <div class="col-left">
                       <?php the_post_thumbnail_url($page->ID , 'my-non-working-size'  ); ?>

                    </div><div class="col-right">
    <?php

    ?></div>
                    <?php
                }
                echo $content; ?></div>
    </div>
        <?php }  ?>
1

You are doing wrong on this part

the_post_thumbnail($page->ID, "my-non-working-size");

For the_post_thumbnail() you don't need to pass post ID, you just have to pass size and attr. More Info - the_post_thumbnail()

So try like this the_post_thumbnail("my-non-working-size"); without passing post ID.

Your markup messed up so I'm not sure your actual markup structure. Here is working example and It tested.

<?php
$args = array(
    'post_type'   => 'homeslide'
);
$mypages = get_posts( $args );
?>
<div class="your-class container">
<?php
foreach ( $mypages as $post ) : setup_postdata( $post ); ?>
<div>
    <h2><a href="<?php echo get_the_permalink(); ?>"><?php the_title(); ?></a></h2>
    <div class="entry">
        <?php if ( has_post_thumbnail() ) { ?>
            <div class="col-left">
            <?php the_post_thumbnail( 'my-non-working-size'  ); ?>
            </div>
            <div class="col-right">
            <?php // ?>
            </div>
        <?php
        } // end if has_post_thumbnail();
        the_content(); ?>
    </div>
</div>
<?php endforeach; 
wp_reset_postdata();?>
</div>

And please read this one if you want to need more about get_posts

  • I think I do need to pass the ID in this case as I'm outside the loop. It doesn't work either way, no featured images are rendered. – Mathias Sep 26 '16 at 20:53
  • 1
    Can you update your post with your code that you using to display thumbnail outside of the loop where the_post_thumbnail() your are using ? – Mohammad Limon Sep 26 '16 at 20:56
  • Sure, just did it. – Mathias Sep 26 '16 at 21:02
  • I'm also update my answer. and if you need different html markup feel free to do this. markup will not causing you any problem here. – Mohammad Limon Sep 26 '16 at 22:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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