Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 列出特定类别的所有附件_Php_Wordpress_Attachment - Fatal编程技术网

Php 列出特定类别的所有附件

Php 列出特定类别的所有附件,php,wordpress,attachment,Php,Wordpress,Attachment,我想使用此脚本列出特定类别帖子的所有附件路径: <?php $args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'category' => 61 ); $the_attachments = get_posts( $args ); if ($the_attachments) {

我想使用此脚本列出特定类别帖子的所有附件路径:

<?php
    $args = array( 
        'post_type'     => 'attachment', 
        'numberposts'   => -1,
        'category'      => 61
    ); 
    $the_attachments = get_posts( $args );
    if ($the_attachments) {
        foreach ( $the_attachments as $post ) {
            setup_postdata($post);
            echo get_attached_file( $post->ID ) . "<br />";
        }
    } wp_reset_query();
    ?>

但问题是,除非我删除“category”参数,否则它不会做任何事情,在这种情况下,它会显示所有附件路径。但我只想要61类的

我已经检查了三遍,确实有一些帖子包含61类的附件

我做错了什么


提前感谢。

类别不是
附件的分类法<代码>帖子
附件
是两种不同的帖子类型,
类别
附在
帖子
上,
附件
帖子
的子类

因此,首先要获得该类别的所有帖子

$cat_posts = get_posts(array(
    'category' => 61,
    'numberposts' => -1
));
创建post ID数组,以便我们可以在
WP\u查询中使用

$parent_ids = array_map('get_post_ids', $cat_posts);

function get_post_ids($post_obj) {
    return isset($post_obj->ID) ? $post_obj->ID : false;
}
现在获取所有父ID的所有子项

$the_attachments = get_posts(array(
    'post_parent__in' => $parent_ids,
    'numberposts' => -1,
    'post_type' => 'attachment'
));
显示附件

if ($the_attachments) {
    foreach ( $the_attachments as $my_attachment ) {
        echo wp_get_attachment_image($my_attachment->ID);
    }
}
注意:
post\u parent\u in
仅在版本
3.6


类别不是
附件
帖子类型的分类法<代码>帖子
附件
是两种不同的帖子类型,
类别
附在
帖子
上,
附件
帖子
的子类

因此,首先要获得该类别的所有帖子

$cat_posts = get_posts(array(
    'category' => 61,
    'numberposts' => -1
));
创建post ID数组,以便我们可以在
WP\u查询中使用

$parent_ids = array_map('get_post_ids', $cat_posts);

function get_post_ids($post_obj) {
    return isset($post_obj->ID) ? $post_obj->ID : false;
}
现在获取所有父ID的所有子项

$the_attachments = get_posts(array(
    'post_parent__in' => $parent_ids,
    'numberposts' => -1,
    'post_type' => 'attachment'
));
显示附件

if ($the_attachments) {
    foreach ( $the_attachments as $my_attachment ) {
        echo wp_get_attachment_image($my_attachment->ID);
    }
}
注意:
post\u parent\u in
仅在版本
3.6


好极了,它就像一个符咒,还有解释的道具。我没有意识到类别没有附件帖子类型的分类法。好极了,它就像一个符咒,还有解释的道具。我没有意识到类别没有附件帖子类型的分类法。