duanche5149 2015-01-07 12:33
浏览 138
已采纳

Wordpress:通过“类别”分类法获取图像

I have the following code and im trying to get the images by category, I have a category name "home-hero" and when I add it into the following code it still displays all the images in the media.

How can i get it to only display all the images under home-hero?

<?php
function get_images_from_media_library() {
  $args = array(
    'taxonomy' => 'home-hero',
    'post_type' => 'attachment',
    'post_mime_type' =>'image',
    'post_status' => 'inherit',
    'order'    => 'DESC'
  );
  $query_images = new WP_Query( $args );
  return $query_images;
}

function display_images_from_media_library() {
  $imgs = get_images_from_media_library();
  $html = '<div id="media-gallery">';
  global $post;
  if ($imgs->have_posts()) {
    while($imgs->have_posts()) {
      $imgs->the_post();
      $html .= '<img src="' . $post->guid . '" alt="" />';
      $html .=  get_the_date('F j, Y');
    }
  }
  $html .= '</div>';
  return $html;
}
echo display_images_from_media_library();
?>

Thanks for taking the time to ready.

  • 写回答

2条回答 默认 最新

  • dpgvdfg321041670 2015-01-07 16:07
    关注

    As I said i a comment, I have a working incomplete solution that I need to finish for a project.

    The problem is, attachments don't have categories, just their parents have, so it is impossible with a simple query to get attachments from a specific category

    You will need to run two queries here, the first query will get all the parent posts from a given category, the second query will use the parent post ID's to get the attachments from all the parent posts given

    In both queries, you just need to get the post ID field, this will dramatically speed up your query, and you will use the returned attachment ID's ($attachments->posts) with wp_get_attachment_image()

    Just a few notes on the code

    • This code was meant to go into a function and $post_type, $taxonomy and $term is supposed to be retrieved dynamically. You can hardcode values to these three variables or also make it dynamic in your function

    • You don't need to use a tax_query, you can simply use the category parameters if you use the build-in taxonomy category. I just used a tax_query so that the function-to-be can be used for any taxonomy. If you leave the tax_query for build-in categories, remember, the taxonomy is category and the term will be the category ID (I've set field to term_id)

    • This code requires at least PHP 5.4. If you have a version older than 5.4, simply change the array wrapper [] to array()

    Here is the code:

    $args1 = [
        'posts_per_page' => -1,
        'fields'         => 'ids',
        'post_type'      => $post_type,
        'tax_query' => [
            [
                'taxonomy'          => $taxonomy,
                'field'             => 'term_id',
                'terms'             => $term,
                'include_children'  => false
            ],
        ],  
    ];
    $post_parents = new WP_Query($args1); 
    
    if( $post_parents->posts ) {    
    
        $args = [
            'post_type'      => 'attachment',
            'post_mime_type' => 'image',
            'post_status'    => 'inherit',
            'fields'         => 'ids',
            'posts__in'      => $post_parents->posts,
        ];
        $attachments = new WP_Query($args); 
    
    }
    ?><pre><?php var_dump($attachments->posts); ?></pre><?php
    

    Hope this helps you in your project

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 怎样才能让鼠标沿着线条的中心线轨迹移动
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?