媒体库中的Wordpress附件

时间:2016-05-11 08:39:46

标签: wordpress tags attachment categories taxonomy

我的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; ?>
  4. 这是插件,供参考:

    // 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' );
    

    这些自定义分类法对我来说是一个新概念,所以我试图了解我的方法。在此先感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

知道了!以下代码正常运行。我定义了wp_prepare_attachment_for_js();get_the_category(),然后将它们用于echo内的函数。

基本上,我有一个查询列出属于“摄影”类别的所有媒体库附件。然后,它检索图库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();
  ?>