【问题标题】:Removing frontend dependancies from WordPress Advanced Custom Fields从 WordPress 高级自定义字段中删除前端依赖项
【发布时间】:2016-01-21 06:24:10
【问题描述】:

我正在尝试使我的主题完全独立于 ACF 的 get_fieldthe_field(所以如果插件被禁用,网站不会中断)。我有一个通过自定义字段'slider_image' 作为内联background-image 加载的图像。我需要调用图像的 URL。是否可以将其加载到内联的 html 中?这是工作代码(带有 ACF 的 the_field):

    <!-- Hero Image -->
    <section class="hero" style="background: url('<?php the_field('slider_image'); ?>') no-repeat center center; background-size: cover;">
    </section>
    <!-- End Hero Image -->

如果插件被禁用,我如何使用get_post_meta重写此语句?

我试过了:

<section class="hero" style="background: url('<?php echo get_post_meta( get_the_ID(), 'slider_image', true ); ?>') no-repeat center center; background-size: cover;">

返回:

<section class="hero" style="background: url('98') no-repeat center center; background-size: cover;">

【问题讨论】:

    标签: php wordpress advanced-custom-fields


    【解决方案1】:

    这必须是附加图片的帖子的 ID。要获取当前图像,您必须使用一些嵌入式 WP 功能,例如 wp_get_attachment_image(如果您想打印图像标签)或 wp_get_attachment_image_src(如果您需要图像属性)

    例如,在您的情况下,当您只想要图像的url 时,您可以执行以下操作:

    $img_id = get_post_meta( get_the_ID(), 'slider_image', true );
    $image_attr = wp_get_attachment_image_src( $img_id, 'large' ); //returns false or array
    if ( $image_attr ) {
        $img_url = $image_attr[0]; ?>
        <section class="hero" style="background: url('<?php echo $img_url; ?>') no-repeat center center; background-size: cover;">
    <?php } ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-17
      • 2017-08-28
      • 2012-08-10
      • 2015-01-21
      • 2017-05-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-25
      相关资源
      最近更新 更多