【问题标题】:How to get a list of uploaded custom-background images in wordpress?如何在 wordpress 中获取上传的自定义背景图片列表?
【发布时间】:2014-06-28 03:55:25
【问题描述】:

我正在构建一个主题,我想使用“超大”脚本来旋转背景图像。 我使用 WP 内置定制器上传了几个背景图片。它们显示在“背景图像”->“已上传”选项卡下的定制器中。

我正在寻找的是检索这些图像的网址的代码 - 就像在定制器中完成的那样,但我会在我的脚本中使用它。

我查看了 /wp-admin/ 中的“customize.php”,但它只调用了一些函数,而该函数的代码在别处。有人知道这段代码的位置吗?

有趣的是,通过定制器上传的图片没有显示在媒体库中...

有人可以帮忙吗?

编辑:换句话说 - 返回通过定制器上传的自定义背景图像的 url 的查询是什么?

谢谢。

【问题讨论】:

    标签: php wordpress background-image


    【解决方案1】:

    如果是静态页面,您可以尝试使用此插件为 acf 插件 repeater field

    <div id="myCarousel" class="carousel slide" data-ride="carousel">
        <div class="carousel-inner">
            <?php if(get_field('carrusel')):?>
                <?php while(has_sub_field('carrusel')): ?>
            <div class="item">
                <img src="<?php $image = get_sub_field('image'); echo $image; ?>" alt="Slide">
            </div>
                <?php endwhile ?>
            <?php endif ?>
        </div>
        <a id="nav-izq" class="left carousel-control" href="#myCarousel" data-slide="prev">
            <img src="<?php PRINT IMAGES ?>/nav-izq.png" alt="">
        </a>
        <a id="nav-der" class="right carousel-control" href="#myCarousel" data-slide="next">
            <img src="<?php PRINT IMAGES ?>/nav-der.png" alt="">
        </a>
    </div>
    

    这是使用 acf 转发器字段创建的引导轮播示例

    【讨论】:

    • 如果我想轻松一点,我可以使用“WP Supersized”插件。我正在尝试在没有任何插件的情况下做到这一点 - 艰难的方式......
    • 我添加了我的引导轮播来测试插件,你可以使用全宽和高度的引导轮播
    • 悉达多,这可能是一种解决方法,尽管我试图在不使用自定义字段的情况下解决这个问题。图像已经在 WP 中上传,并存储在数据库中的某个位置。我只需要一个适当的查询来找到它们。
    • $query_images_args = array('post_type' => 'attachment', 'post_mime_type' =>'image', 'post_status' => 'inherit', 'posts_per_page' => -1, ); $query_images = new WP_Query( $query_images_args ); $images = 数组(); foreach ( $query_images->posts as $image) { $images[]= wp_get_attachment_url( $image->ID ); }
    • 悉达多,这将返回所有图像的网址。我只需要通过定制器上传的图像作为自定义背景。
    【解决方案2】:

    想通了!我在整个 WP 数据库中搜索了自定义背景图像的 ID。原来他们在“post_meta”表中有一个特殊的键:“_wp_attachment_is_custom_background”

    在 WP_query 中使用这个键,我能够准确地找到通过定制器上传为自定义背景的图像。

    为了大家的利益,这是我在“header.php”中使用的代码:

    <script type="text/javascript">
        jQuery(function($){
            $.supersized({
                // Functionality
                slide_interval    : 5000,
                transition        : 1, 
                transition_speed  : 2000,               
                slide_links       : 'blank',
                slides            : [
    <?php 
    $args = array( 'post_type' => 'attachment', 
                   'post_mime_type' =>'image', 
                   'post_status' => 'inherit',
                   'meta_query' => array (
                      array (
                        'key' => '_wp_attachment_is_custom_background',
    ),) ); 
    $bk_images = new WP_Query( $args ); 
    $in = 0; 
    $in_end = sizeof($bk_images->posts);
    foreach ( $bk_images->posts as $bk) { 
        $in++;
        echo "   {image : '" . wp_get_attachment_url( $bk->ID ) . "', title : 'Title', url : 'url'}"; 
        if ($in<$in_end) { echo ",\n"; } else { echo "\n"; }
    } ?>
                                     ]
            });
        });
    </script>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-24
      • 1970-01-01
      • 1970-01-01
      • 2011-09-11
      • 2018-08-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多