5

i have created a custom post named Products.

register_post_type( 'products',
    array(
        'labels' => array(
            'name' => __( 'Products' ),
            'singular_name' => __( 'Product' )
        ),
    'public' => true,
    'has_archive' => true,
    'supports' => array( 'title', 'editor', 'thumbnail' )
);

I have also created a php file named archive-products.php and made it into a template.

In Wordpress I have created a page named Products and selected the products template.

On that static page (that uses the archive template) I have uploaded a image into the Featured Image panel.

In my header I have the code:

echo get_the_post_thumbnail();

But this echos the Featured image of the last custom post in the list (all the products posts have a featured image as well), not the Featured image of the static/archive page, which is what i want. Is this possible to achieve?

Thanks!

4

1 回答 1

3

我做了同样的事情,并遇到了解决我的问题的以下答案:https ://wordpress.stackexchange.com/a/175228

  1. 将您的自定义帖子类型存档模板另存为页面。

    例如,page-products.php

  2. 本地备份并从您的服务器中删除您的自定义帖子类型存档模板。

  3. 使用 显示图像the_post_thumbnail(),使用 将其放入变量中get_the_post_thumbnail(),或将其设置为背景图像,并在其上添加页面标题:

    $bg = wp_get_attachment_image_src( get_post_thumbnail_id( $page->ID ), 'full' ); if( is_page('products') ) : ?> <div style="background: url('<?php echo $bg[0]; ?>') repeat center center #fbfbfb; background-size:cover;"> <?php the_title( '<h1 class="page-title">', '</h1>' ); ?> </div> <?php endif; ?>

  4. 保存您的永久链接并刷新您的页面。

这对我有用。希望它可以帮助某人。:)

于 2015-08-23T08:01:45.370 回答