Search code examples
wordpressphp-7timber

Wordpress dump: featured image not working (frontend only)


I am trying to get the featured image (thumbnail) to show up on the frontend. The backend works like a charm. However i'm having an issue getting the thumbnail for this custom post type and also the default posts. It works for single.twig files, but not on the home.twig (the front-page equivalent). When I dump() the variables i get everything i need to show up, except the featured image.

Where could the issue be?

  • Wordpress: 5.2.2
  • Timber: 1.9.2
  • ACF PRO: 5.8.3
  • no other plugins

home.twig (simplified)

{% for event in events %}
  {{ event.thumbnail.src }} //does not work
  {{ event.title }} //works
{% endfor %}

index.php

$context['events'] = get_posts(array(
    'numberposts'   => -1,
    'post_type'     => 'Events',
    'orderby' => 'DATE',
    'order' => 'DESC'
));

functions.php

add_action( 'init', 'create_posttype' );
/*
* Creating a function to create our CPT
*/

//START CUSTOM POST TYPE
function custom_post_type() {

// Set UI labels for Custom Post Type
    $labels = array(
        'name'                => _x( 'Events', 'Post Type General Name', 'the-template' ),
        'singular_name'       => _x( 'Event', 'Post Type Singular Name', 'the-template' ),
        'menu_name'           => __( 'Events', 'the-template' ),
        'parent_item_colon'   => __( 'Parent Event', 'the-template' ),
        'all_items'           => __( 'All Events', 'the-template' ),
        'view_item'           => __( 'View Event', 'the-template' ),
        'add_new_item'        => __( 'Add New Event', 'the-template' ),
        'add_new'             => __( 'Add New', 'the-template' ),
        'edit_item'           => __( 'Edit Event', 'the-template' ),
        'update_item'         => __( 'Update Event', 'the-template' ),
        'search_items'        => __( 'Search Event', 'the-template' ),
        'not_found'           => __( 'Not Found', 'the-template' ),
        'not_found_in_trash'  => __( 'Not found in Trash', 'the-template' ),
    );

// Set other options for Custom Post Type

    $args = array(
        'label'               => __( 'events', 'the-template' ),
        'description'         => __( 'Event news', 'the-template' ),
        'labels'              => $labels,
        // Features this CPT supports in Post Editor
        'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'revisions', 'custom-fields', ),
        // You can associate this CPT with a taxonomy or custom taxonomy.
        'taxonomies'          => array( 'genres' ),
        /* A hierarchical CPT is like Pages and can have
        * Parent and child items. A non-hierarchical CPT
        * is like Posts.
        */
        'hierarchical'        => false,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 5,
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'page',
    );

    // Registering your Custom Post Type
    register_post_type( 'Events', $args );

}

/* Hook into the 'init' action so that the function
* Containing our post type registration is not
* unnecessarily executed.
*/

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

Solution

  • When you use get_posts(), you’re getting normal WordPress posts, you need Timber posts. Use Timber::get_posts() instead of get_posts() to get back instances of Timber\Post instead of WP_Post.

    index.php

    $context['events'] = Timber::get_posts( array(
        'numberposts' => -1,
        'post_type'   => 'Events',
        'orderby'     => 'DATE',
        'order'       => 'DESC'
    ) );