Subrata Sarkar
Subrata Sarkar

Reputation: 3064

Featured Image for Custom Post Type is not showing inside loop

I have a Custom Post Type. Each post has a Featured Image. In my index.php I am want to show up the title of each post with its Featured image. The following code shows the title but not the image.

<?php $query = new WP_Query(
      array(
        'post_type' => 'platform',
        'status'    => 'publish'
      ) );
      if ( $query->have_posts() ) {
         while ($query->have_posts()) {
            $query->the_post(); ?>
            <p><?php the_title(); ?></p>
            <p><? if( has_post_thumbnail() ) {
                  the_post_thumbnail();
               } ?></p>
       <?php }
      }

      wp_reset_postdata();

In wp_postmeta table there is a _thumbnail_id associated with each post id. In admin, the images are showing as well.

What I am doing wrong?

Update

CPT registration code:

add_action('init', 'yrc_register_cpt_emfluence_platform_list', 0);

/**
 * Create and register new Custom Post Type: Emfluence Platform List
 * Fields: Name, Email, Platform List ID, Featured Image
 */
function yrc_register_cpt_emfluence_platform_list() {
    $labels = array(
        'name'                  => __('Platforms', 'yrc_csrvtool'),
        'singular_name'         => __('Platform', 'yrc_csrvtool'),
        'menu_name'             => __('Platforms', 'yrc_csrvtool'),
        'all_items'             => __('All Platforms', 'yrc_csrvtool'),
        'view_item'             => __('View Platform', 'yrc_csrvtool'),
        'ad_new_item'           => __('Add New', 'yrc_csrvtool'),
        'add_new'               => __('Add New', 'yrc_csrvtool'),
        'edit_item'             => __('Edit Platform', 'yrc_csrvtool'),
        'update_item'           => __('Update Platform', 'yrc_csrvtool'),
        'search_items'          => __('Search Platforms', 'yrc_csrvtool'),
        'not_found'             => __('Not found', 'yrc_csrvtool'),
        'not_found_in_trash'    => __('Not found in trash', 'yrc_csrvtool'),

    );

    $args = array(
        'labels'                =>  $labels,
        'description'           =>  __(' Platform List', 'yrc_csrvtool'),
        'supports'              => array('title', 'thumbnail' ),
        'hierarchical'          => false,
        'public'                => true,
        'show_in_menu'          => true,
        'menu_icon'             => 'dashicons-email',
        'show_in_nav_menus'     => true,
        'menu_position'         => 5,
        'exclude_from_search'   => true,
        'publicly_queryable'    => false,
        'capability_type'       => 'post',
    );

    register_post_type('platform', $args);
}

And in the theme I have theme_support for thumbnails as well:

add_theme_support('post-thumbnails');

The images are very much visible in admin, but not when I use the_post_thumbnail() inside loop.

Upvotes: 1

Views: 1332

Answers (2)

Sajjad Hossain Sagor
Sajjad Hossain Sagor

Reputation: 518

The Problem is in your php syntax; <? if( has_post_thumbnail() ) { >> Here you are missing php after <? So nothing is interpreted inside this block of code by php. So Correct syntax is ....

<?php $query = new WP_Query(
  array(
    'post_type' => 'platform',
    'status'    => 'publish'
  ) );
  if ( $query->have_posts() ) {
     while ($query->have_posts()) {
        $query->the_post(); ?>
        <p><?php the_title(); ?></p>
        <p><?php if( has_post_thumbnail() ) { //missing php after <? here
              the_post_thumbnail();
           } ?></p>
   <?php }
  }

  wp_reset_postdata();

Upvotes: 1

Souvik Sikdar
Souvik Sikdar

Reputation: 775

I have seen that, you have start php in wrong way before this bellow code

if( has_post_thumbnail() ) {

There is only <?, but it should be <?php Please check your first code. Thanks

Upvotes: 2

Related Questions