GET_POSTS$args不接受数组作为条件之一吗?

时间:2015-09-17 作者:Jon Brantingham

目标是使用WooCommerce产品中的标签,列出WooCommerce产品下面的附件。如果我在以下位置手动输入标签,一切正常\'product_tag\', 但是当我尝试使用数组时$producttags, 为什么不起作用?

add_action( \'woocommerce_after_single_product_summary\', \'precon_drawing_attachments\' );
function precon_drawing_attachments() {
    $productterms = get_the_terms( $post->ID, \'product_tag\' );
    $producttags = array();
    foreach ( $productterms as $productterm ) {
        $producttags[] = $productterm->name;
    }
    $args = array(
        \'post_mime_type\' => \'application/pdf\',
        \'post_type\'      => \'attachment\',
        \'product_tag\'    => array( $producttags ),
        \'posts_per_page\' => -1,
        \'post_status\'    => \'any\',
        \'post_parent\'    => null );
    $attachments = get_posts( $args );
    if ( $attachments ) {
        echo \'<div class="drawings-list">\';
        echo \'<ul>\';
        foreach ( $attachments as $post ) {
            echo \'<li class="drawing-li">\';
            echo \'<img class="drawing-icon" src="\';
            bloginfo( \'stylesheet_directory\' );
            echo \'/images/Adobe_PDF_file_icon_32x32.png"\';
            echo \' />\';
            echo \'<span class="drawing-link">\';
            echo the_attachment_link( $post->ID, false );
            echo \'</span>\';
            echo \'</li>\';
        }
        echo \'</ul></div>\';
    }
}

1 个回复
最合适的回答,由SO网友:jzatt 整理而成

我认为你的问题在于\'product_tag\' => array( $producttags ),

$producttags 已经是一个数组,因此在本例中,它被嵌套到子数组中。将参数改为以下内容:

\'product_tag\' => $producttags,

相关推荐