简体   繁体   English

从Wordpress中的媒体库中检索特定图像

[英]Retrieving specific images from Media Library in Wordpress

I developed a theme in which I show a Bootstrap carousel filled with sponsor images. 我开发了一个主题,其中展示了充满赞助商图片的Bootstrap轮播。 At the moment the sponsor-images are hardcoded like this: 目前,赞助商图片已按照以下方式进行了硬编码:

<div class="item active">
  <div class="row outerDiv">
    <div class="col-xs-12 innerDiv">
      <a href="<?php echo bloginfo('url'); ?>/recruiting-messe/"><img class="carImg img-responsive" src="<?php bloginfo('template_url')?>/images/Sponsoren/PremiumSponsoren.jpg" alt="Premium Sponsoren" style="margin: 0 auto;"></a>
    </div>
  </div>
</div>

I want my authors to be able to change the images without being required to dig into the code itself, but I don't know how to go about it. 我希望我的作者能够更改图像而无需深入研究代码本身,但是我不知道该怎么做。 I thought about maybe retrieving images uploaded into the media library with a certain naming convention or a caption "sponsor" with a loop? 我考虑过要使用某种命名约定或带有循环的标题“赞助商”来检索上传到媒体库的图像吗? What would be the best way to implement something like this and how? 实现这样的最佳方式是什么?如何实现? Thanks in advance! 提前致谢!

Something like this. 这样的事情。

$attachment_meta = wp_get_attachment(your_attachment_id);
$caption = $attachment_meta['caption'];

if($caption == 'sponsor') :
// show me the money
endif;

What about just use a plug in for it? 仅使用插件呢? Or designate a post,attach a gallery, and use gallery short code for a simple bootstrap carousel to use? 还是指定一个帖子,附加一个画廊,并使用画廊的短代码来使用一个简单的引导轮播? Hope this helps. 希望这可以帮助。

Maybe you can do the following, use some meta fields plugin like this and add image field where the author can choose which image is sponsored. 也许您可以执行以下操作,使用像这样的一些meta字段插件并添加image字段,以便作者可以选择赞助哪个图片。 Then you can do something like this 然后你可以做这样的事情

if($current_attachment_id == get_field('image_field')){
     //Do the trick
}

Thanks to everyone who tried to help. 感谢所有尝试提供帮助的人。 I solved the problem by querying for attachements with a specific title "SponsorenBanner". 我通过查询带有特定标题“ SponsorenBanner”的附件来解决了这个问题。

$query_images_args = array(
      'post_type'      => 'attachment',
      'post_mime_type' => 'image',
      'post_status'    => 'inherit',
      'posts_per_page' => - 1,
      'title' => 'SponsorenBanner',
      'order' => 'ASC',
      );

    $query_images = new WP_Query( $query_images_args );

    $images = array();
    foreach ( $query_images->posts as $image ) {

      $images[] = wp_get_attachment_url( $image->ID );

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM