WordPress自定义文章类型(CPT)特色图片无法显示求助
自定义文章类型(CPT UI创建)特色图片无法显示问题
问题概况
- 已查阅Stack Overflow上《Featured Images in WP not showing》和《Custom Posts on Wordpress not showing》相关回答
- 可正常获取CPT UI创建的
artwork类型自定义文章的标题、永久链接,但特色图片无法显示
已完成的配置
- 在主题
functions.php顶部添加特色图片支持代码:
function custom_theme_features() { // Add theme support for Featured Images add_theme_support( 'post-thumbnails' ); } // Hook into the 'after_setup_theme' action add_action( 'after_setup_theme', 'custom_theme_features' );
- 在CPT UI的「编辑文章类型」窗口底部,已勾选“featured image”选项
测试代码及运行结果
$recent_posts = wp_get_recent_posts(array('post_type' => 'artwork', 'numberposts' => '10')); foreach ($recent_posts as $recent) { $recentID = $recent['ID'] ; echo ' --- recentID=' . $recentID; // 返回ID,正常 echo ' --- post_title=' . $recent["post_title"]; // 返回自定义文章标题,正常 echo ' --- get_permalink=' . get_permalink($recentID) ; // 返回正确的永久链接,正常 echo ' --- get_post_thumbnail_id=' . get_post_thumbnail_id($recentID); // 返回0 echo ' --- get_post_thumbnail_id=' . get_post_thumbnail_id($recent); // 无返回内容 echo ' --- acf_get_post_thumbnail=' . acf_get_post_thumbnail($recentID)['url'] ; // 无返回内容(尝试多种方法) echo ' --- get_url_from_thumbnail_id=' . get_url_from_thumbnail_id($recentID); // 无返回内容 the_post_thumbnail(); // 多次返回同一张图片,该图片为普通文章的特色图片 echo ' --- wp_get_attachment_image='.wp_get_attachment_image($attachment_id, 'thumbnail'); // 无返回内容 }
内容的提问来源于stack exchange,提问作者Francis
相关产品推荐
相关产品推荐

