WordPress.org

Ready to get started?Download WordPress

Forums

Multiple Post Thumbnails
[resolved] Display thumbnails from more than one custom post type (4 posts)

  1. djvern
    Member
    Posted 2 years ago #

    I've registered a secondary post thumbnail for multiple post types using the following code:

    if (class_exists('MultiPostThumbnails')) {
        $types = array('events', 'exhibitions');
        foreach($types as $type) {
            $thumb = new MultiPostThumbnails(array(
                'label' => 'Secondary Image',
                'id' => 'secondary-image',
                'post_type' => $type
                )
            );
        }
    }

    However, how can I show thumbnails from these multiple post types in the same place? (For example I have a 'What's on' page that shows content from both Events and Exhibitions). I've tried using an array, eg:

    <?php if (class_exists('MultiPostThumbnails')
        && MultiPostThumbnails::has_post_thumbnail(array('events','exhibitions'), 'secondary-image')) :
            MultiPostThumbnails::the_post_thumbnail(array('events','exhibitions'), 'secondary-image'); endif; ?>

    But it doesn't work. Any ideas? Ideally I'd like to not have to declare what post type the thumbnail is associated with, just like how the regular post thumbnail works.

    Thanks in advance for any help!
    Dave

    http://wordpress.org/extend/plugins/multiple-post-thumbnails/

  2. stuartobrien
    Member
    Posted 2 years ago #

    Great plugin!

    Thanks for the question above djvern. I need the same things as above.
    My first question though is if the above works?
    'post_type' => array('events', 'exhibitions')

    I don't believe that the constructor function takes and array argument for the 'post' type. It only takes one at a time, so you need to register one MultiPostThumbnail for each post type. Chris, is this correct?

    Second question/request is for Chris if the plugin could be made to take an array, so it can register for multiple post types?

    Third question/request is the same as above, that the template function could be made to work without needing to specify the post type.

    I am building a Homepage Slider that pulls from 3 post types, and I now have to check for each image individually.

    if ( class_exists('MultiPostThumbnails') ){
    	$img_src = MultiPostThumbnails::get_post_thumbnail_url('radio-shows','slider-image');
    	if(!$img_src) MultiPostThumbnails::get_post_thumbnail_url('artists','slider-image');
    	if(!$img_src) MultiPostThumbnails::get_post_thumbnail_url('news','slider-image');
    }
    if ( !$img_src ){
    	$img_object = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'thumbnail' );
    	$img_src = $img_object['0'];
    }

    Thanks.

  3. Chris Scott
    Member
    Plugin Author

    Posted 2 years ago #

    @Dave: you can't use an array but have to specifically tell it what the post type is. So, you'll need to loop through the post types you want to display and call the_post_thumbnail. e.g.

    if (class_exists('MultiPostThumbnails')) {
      foreach (array('events','exhibitions') as $mpt_post_type) {
        MultiPostThumbnails::the_post_thumbnail($mpt_post_type, 'secondary-image');
      }
    )
  4. Chris Scott
    Member
    Plugin Author

    Posted 2 years ago #

    @stuartobrien: I had originally wanted the constructor to take an array of post types, but for reasons I've long since forgotten, it wasn't either doable or desirable.

    re: your second question, see my reply above to see if that helps.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic