来自媒体库的 Wordpress 附件

Wordpress Attachments from Media Library

我的 wordpress 附件图像有类别和标签。我想在照片库中调用此信息。

好消息:在循环中调用时会出现正确的图像。坏消息:我无法调用图库中的特定数据。问题全部大写:

<?php
$the_query = new WP_Query(array(
    'post_type' => 'attachment',
    'post_status' => 'inherit',
    'category_name' => 'architecture'
    ));
while ( $the_query->have_posts() ) :
    $the_query->the_post();
?>
<figure class="gallery-photo" itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject" data-groups='["all", "PHP ATTACHMENT TAG"]'>
    <a class="photo-link" href="<?php wp_get_attachment_image (); ?>" itemprop="contentUrl" data-size="CALL PHP ATTACHMENT IMAGE WIDTH & HEIGHT">
        <img src="<?php wp_get_attachment_url ('full'); ?>" itemprop="thumbnail" />
        <figcaption itemprop="caption description">PHP ATTACHMENT CAPTION</figcaption>
        <div class="photo-title"><h2>PHP ATTACHMENT IMAGE TITLE</h2></div>
    </a>
</figure>
<?php
endwhile;
wp_reset_postdata();
?>

一些我尝试过但没有奏效的事情:

  1. <?php $meta = wp_get_attachment_metadata($image->ID, true); echo '.$meta[width]."x".$meta[height].';?>
  2. <?php $description = $post->post_content; echo $description;?>
  3. <?php $image->post_title; ?>

这里是插件,供参考:

// Custom media taxonomies
function add_categories_to_attachments() {
    register_taxonomy_for_object_type( 'category', 'attachment' );
}
add_action( 'init' , 'add_categories_to_attachments' );

function add_tags_to_attachments() {
    register_taxonomy_for_object_type( 'post_tag', 'attachment' );
}
add_action( 'init' , 'add_tags_to_attachments' );

这些自定义分类法对我来说是一个新概念,所以我正在努力学习如何使用它们。在此先感谢您的帮助!

知道了!下面的代码正在工作。我定义了 wp_prepare_attachment_for_js();get_the_category(),然后将它们用于 echo.

中的函数

本质上,我有一个查询,列出属于类别 "photography." 的所有媒体库附件,然后,它检索有关每个附件的信息(标题、标题、宽度、高度,url,类别名称)在画廊 html 容器内。

    <?php 
   $the_query = new WP_Query(array(
    'post_type' => 'attachment',
    'post_status' => 'inherit',
    'category_name' => 'photography'
    )); 

   while ( $the_query->have_posts() ) : 
   $the_query->the_post();
  ?>
    <?php
 $attachment_data = wp_prepare_attachment_for_js( $attachment->ID );
 $category = get_the_category($attachment->ID);
      echo '<figure class="gallery-photo" itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject" data-groups='. esc_attr('["all","'.$category[0]->cat_name.'"]').'>';
      echo'<a class="photo-link" href="'.$attachment_data['url'].'" itemprop="contentUrl" data-size="'.$attachment_data['width'].'x'.$attachment_data['height'].'">';
      echo'<img src="'.wp_get_attachment_url ('medium').'" itemprop="thumbnail"/>';
      echo'<div class="photo-title"><h2>'.$attachment_data['title'].'</h2></div></a>';
      echo'<figcaption itemprop="caption description">'.$attachment_data['caption'].'</figcaption></figure>';?>
   <?php 
   endwhile; 
   wp_reset_postdata();
  ?>