【问题标题】:Wordpress featured image request returning unknownWordPress 特色图片请求返回未知
【发布时间】:2018-08-04 20:35:23
【问题描述】:

我通常用 javascript 编写,但我目前正在尝试为一个副项目设置一个 Wordpress 主题。我找到了一些资源,概述了如何通过内联样式将一组特色图像用作 div 背景图像,但它们有点旧。我目前正在尝试找到一个basic method,并尝试更新它以匹配当前的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>

该部分正在渲染,但开发工具显示“背景:url((未知))”

  1. 有人可以在这里用 php 指出一个糟糕的 javascript-er 吗?
  2. 如果有人更熟悉 wordpress,他们可以帮我确认“大”是默认图像大小,还是我需要注册?

【问题讨论】:

    标签: php wordpress wordpress-theming


    【解决方案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

    【讨论】:

    • 返回一个链接,而不是“未知”,但它指向“*/wp-includes/images/media/default.png”,这是一个空图像。
    • :facepalm: 错误的帖子,一个渲染没有将图像设置为特色。此解决方案有效。
    • 好交易。如果对您有用,您可以将我的解决方案标记为正确。
    【解决方案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>
    

    【讨论】:

      猜你喜欢
      • 2015-05-01
      • 1970-01-01
      • 2012-10-22
      • 2019-07-31
      • 2012-09-17
      • 1970-01-01
      • 2015-04-28
      • 1970-01-01
      • 2012-05-21
      相关资源
      最近更新 更多