【问题标题】:Coding Image gallery, PHP get WP 'Featured' img编码图像库,PHP 获取 WP 'Featured' img
【发布时间】:2013-07-12 22:42:58
【问题描述】:

我目前正在使用 yoxview 图片库。 (www.yoxigen.com/yoxview/) 但这并不重要。我目前在 Wordpress 安装前设置的一个小型启动主页上对此进行了编码。我在这里尝试做的是允许通过 Yoxview 的图片库以原子方式抓取和拉取由 Wordpress 帖子分配的“精选”图片集。我假设我需要一些 PHP 来代替 href=""src="" 但我不确定是什么。本质上,只是希望画廊动态运行并使用 Wordpress 帖子中设置的特色图片作为拇指填充。谁能帮我写这个?干杯。

<div class="thumbnails yoxview">

    <a href="/wp-content/uploads/2013/06/broken-beer-bottle.jpg"><img src="/wp-content/uploads/2013/06/broken-beer-bottle.jpg" alt="First" title="The first image" class="thumb"/></a>

    <a href="/wp-content/uploads/2013/06/29214_116842028347050_6051723_n.jpg"><img src="/wp-content/uploads/2013/06/29214_116842028347050_6051723_n.jpg" alt="" title="The SECOND image" class="thumb" style="max-height: 100px;"/></a> 

    <a href="/wp-content/uploads/2013/07/images.jpg"><img src="/wp-content/uploads/2013/07/images.jpg" alt="" title="The Third image" class="thumb" style="max-height: 100px;"/></a>

    <a href="/wp-content/uploads/2013/07/solocups.jpg"><img src="/wp-content/uploads/2013/07/solocups.jpg" alt="" title="The Third image" class="thumb" style="max-height: 100px;"/></a>

</div> 

可能是更好的改写;如何在 PHP 中编写“获取 Wordpress 特色图片”?

【问题讨论】:

  • 您想要单张特色图片吗?或者附在一篇文章中的图片?

标签: php wordpress


【解决方案1】:

这是我用来获取单个帖子的特色图片和附加图片的代码:

  $args = array(
    'order' => 'ASC',
    'post_mime_type' => 'image',
    'post_parent' => $post->ID,
    'post_status' => null,
    'post_type' => 'attachment',

  );

  $upload_dir = wp_upload_dir();
  $upload_url = $upload_dir['baseurl'];


  // Get img data
  $attachments    = get_children( $args );
  $images = array();

  foreach($attachments as $attachment) {
    $image_attributes = wp_get_attachment_image_src( $attachment->ID, 'thumbnail' );
    $img['thumb'] = $image_attributes[0];
    $image_attributes = wp_get_attachment_image_src( $attachment->ID, 'large' );
    $img['large'] = $image_attributes[0];
    array_push($images,$img);
  }

  $featured_img = wp_get_attachment_url(get_post_thumbnail_id($post->ID));
  $featured_img_thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'thumbnail' );

【讨论】:

  • 非常感谢@Steven。你把这段代码放在哪里,你用什么在画廊标记中输出它?干杯,卡姆。
  • 在我的 single.php 文件的顶部。要输出它,只需编写普通的 HTML 和 PHP &lt;img src="&lt;?php echo $featured_img; ?&gt;"/&gt;。如果您是 PHP 和 WP 新手,那么您应该查看更多关于如何使用各种 WP 功能的教程。
  • 感谢您的建议 - 将尝试一下。
猜你喜欢
  • 2014-03-07
  • 2013-02-27
  • 2019-06-13
  • 2015-12-17
  • 2017-01-08
  • 1970-01-01
  • 2014-11-16
  • 1970-01-01
  • 2020-04-20
相关资源
最近更新 更多