Wordpress - 自定义帖子存档页面上的特色图片

Kea*_*eat 5 wordpress custom-post-type

我创建了一个名为 Products 的自定义帖子。

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

我还创建了一个名为archive-products.php的 php 文件并将其制作成模板。

在 Wordpress 中,我创建了一个名为Products的页面并选择了 products 模板。

在该静态页面(使用存档模板)上,我已将图像上传到“精选图像”面板中。

在我的标题中,我有代码:

echo get_the_post_thumbnail();
Run Code Online (Sandbox Code Playgroud)

但这与列表中最后一个自定义帖子的特色图片相呼应(所有产品帖子也有特色图片),而不是静态/存档页面的特色图片,这正是我想要的。这有可能实现吗?

谢谢!

小智 3

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

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

    例如,页面-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. 保存您的永久链接并刷新您的页面。

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