Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wordpress转储:功能图像不工作(仅前端)_Wordpress_Php 7_Timber - Fatal编程技术网

Wordpress转储:功能图像不工作(仅前端)

Wordpress转储:功能图像不工作(仅前端),wordpress,php-7,timber,Wordpress,Php 7,Timber,我正试图让特色图像(缩略图)显示在前端。后端就像一个魅力。但是,我在获取此自定义帖子类型的缩略图以及默认帖子时遇到了一个问题。它适用于single.twig文件,但不适用于home.twig(相当于首页)。当我dump()变量时,我得到了所有需要显示的内容,除了特色图片 问题出在哪里 Wordpress:5.2.2 木材:1.9.2 ACF PRO:5.8.3 没有其他插件 home.twig(简化版) index.php $context['events'] = get_posts(arr

我正试图让特色图像(缩略图)显示在前端。后端就像一个魅力。但是,我在获取此自定义帖子类型的缩略图以及默认帖子时遇到了一个问题。它适用于
single.twig
文件,但不适用于
home.twig
(相当于首页)。当我
dump()
变量时,我得到了所有需要显示的内容,除了特色图片

问题出在哪里

  • Wordpress:5.2.2
  • 木材:1.9.2
  • ACF PRO:5.8.3
  • 没有其他插件
home.twig(简化版)

index.php

$context['events'] = get_posts(array(
    'numberposts'   => -1,
    'post_type'     => 'Events',
    'orderby' => 'DATE',
    'order' => 'DESC'
));
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 );
functions.php

$context['events'] = get_posts(array(
    'numberposts'   => -1,
    'post_type'     => 'Events',
    'orderby' => 'DATE',
    'order' => 'DESC'
));
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::get_posts()
而不是
get_posts()
来获取
Timber\Post的实例,而不是
WP_Post

index.php

$context['events'] = get_posts(array(
    'numberposts'   => -1,
    'post_type'     => 'Events',
    'orderby' => 'DATE',
    'order' => 'DESC'
));
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 );
$context['events']=Timber::get_posts(数组)(
“numberposts”=>-1,
“post_类型”=>“事件”,
'orderby'=>'DATE',
“订单”=>“描述”
) );

谢谢你的回复,我试过了,但还是一片空白。甚至不是空的。我也直接试过了,也没用。(像这样{{TimberImage(event.thumboil)}})嗯。。。不知道,谢谢你!我完全忘了!这几秒钟就解决了我的问题。