个性化阅读
专注于IT技术分析

WordPress特色图片请求返回未知

点击下载

我通常用JavaScript编写, 但是我目前正在尝试为辅助项目设置Wordpress主题。我发现了一些资源, 概述了如何通过内联样式将设置的特色图像用作div背景图像, 但是它们有些陈旧。我目前正在尝试找到的一种基本方法, 并尝试对其进行更新以匹配当前的Wordpress Docs, 但是我对PhP还是陌生的。

在我的函数文件中, 我有:

add_theme_support( 'post-thumbnails' );

在我的模板文件中, 我有:

<?php $bgImg = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'large'); ?>

<section style="background: url('<?php echo $bgImg[0]; ?>') no-repeat;">

</section>

该部分正在渲染, 但开发工具显示” background:url((unknown))”

  1. 有人可以在这里用php向正确的方向指出可怜的javascript-er吗?
  2. 如果有人更熟悉wordpress, 他们可以为我确认”大”是默认图像大小, 还是我需要注册它?

#1


我使用类似的方法:

$thumb_id = get_post_thumbnail_id();
$thumb_url_array = wp_get_attachment_image_src($thumb_id, 'thumbnail-size', true);
$thumb_url = $thumb_url_array[0];

<div class="image row" style="background: url('<? echo $thumb_url;  ?>'); ">content</div

编辑:这是默认情况下图像尺寸的列表。 https://codex.wordpress.org/Post_Thumbnails#Thumbnail_Sizes


#2


使用以下代码代替你的代码。

<?php 
$featuredImg = get_the_post_thumbnail_url($post->ID, 'full');
if($featuredImg) {
    $bgImg = $featuredImg;
} else {
    $bgImg = get_template_directory_uri().'/images/default_bg.jpg';
}
?>
<section style="background: url('<?php echo $bgImg; ?>') no-repeat;">
</section>
赞(0)
未经允许不得转载:srcmini » WordPress特色图片请求返回未知

评论 抢沙发

评论前必须登录!