Wordpress 转储:特色图片不起作用(仅限前端)
Wordpress dump: featured image not working (frontend only)
我正在尝试让特色图片(缩略图)显示在前端。后端就像一个魅力。但是,我在获取此自定义 post 类型和默认 post 类型的缩略图时遇到问题。它适用于 single.twig
文件,但不适用于 home.twig
(相当于首页)。当我 dump()
变量时,我得到了我需要显示的所有内容,除了特色图片。
问题可能出在哪里?
- WordPress:5.2.2
- 木材:1.9.2
- ACF 专业版:5.8.3
- 没有其他插件
home.twig(简体)
{% 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 );
试试这个
{% for event in events %}
{% set image = TimberImage(event.thumbnail) %}
{{ image.src }}
{{ event.title }}
{% endfor %}
当您使用 get_posts()
时,您会收到普通的 WordPress 帖子,您需要 Timber 帖子。使用 Timber::get_posts()
而不是 get_posts()
来取回 Timber\Post
而不是 WP_Post
.
的实例
index.php
$context['events'] = Timber::get_posts( array(
'numberposts' => -1,
'post_type' => 'Events',
'orderby' => 'DATE',
'order' => 'DESC'
) );
我正在尝试让特色图片(缩略图)显示在前端。后端就像一个魅力。但是,我在获取此自定义 post 类型和默认 post 类型的缩略图时遇到问题。它适用于 single.twig
文件,但不适用于 home.twig
(相当于首页)。当我 dump()
变量时,我得到了我需要显示的所有内容,除了特色图片。
问题可能出在哪里?
- WordPress:5.2.2
- 木材:1.9.2
- ACF 专业版:5.8.3
- 没有其他插件
home.twig(简体)
{% 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 );
试试这个
{% for event in events %}
{% set image = TimberImage(event.thumbnail) %}
{{ image.src }}
{{ event.title }}
{% endfor %}
当您使用 get_posts()
时,您会收到普通的 WordPress 帖子,您需要 Timber 帖子。使用 Timber::get_posts()
而不是 get_posts()
来取回 Timber\Post
而不是 WP_Post
.
index.php
$context['events'] = Timber::get_posts( array(
'numberposts' => -1,
'post_type' => 'Events',
'orderby' => 'DATE',
'order' => 'DESC'
) );