不复制POST_PARENT和显示标记图像的WP_QUERY附件

时间:2014-11-18 作者:Stephen S.

我在我的网站上的许多媒体上传上都贴上了标签,上面有各种产品的名称。目标是获得一个贴子列表,这些贴子已经标记了每个产品名称,但不会重复父贴子。

理想情况下,我可以显示循环1中的一个附件(实际显示标记的产品),并带有指向其post\\u父级的链接,但我只能从循环2中获取帖子缩略图(这些缩略图并不总是显示产品)以达到这一点。

我的大脑被炸了。有什么想法吗?

循环1-查询带有特定标记的附件。

$media_args = array(
    \'post_type\' => \'attachment\',
    \'posts_per_page\' => -1,
    \'tax_query\' => array(
        array(
            \'taxonomy\' => \'products\',
            \'field\' => \'term_id\',
            \'terms\' => $product_tag
                    )
                )
            );
$media_items = new WP_Query($media_args); 

if ($media_items->have_posts()) {
    $parent = array();
    while ($media_items->have_posts()) : $media_items->the_post();
        // getting the post_parent and cleaning to weed out duplicates
        $parent[] = get_post_field(\'post_parent\', $post->ID);
        $parent_clean = array_unique($parent);
    endwhile;
}
循环2-查询唯一帖子列表

$product_post_args = array(
    \'post_type\' => \'post\',
    \'posts_per_page\' => -1,
    \'post__in\' => $parent_clean
    );
$product_posts = new WP_Query($product_post_args);

if ($product_posts->have_posts()) { ?>
   while ($product_posts->have_posts()) : $product_posts->the_post();

   $img = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), \'browse\' );

   echo $img[0];

   endwhile;
   } 

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

在用一些新鲜的眼光重新审视它之后,我决定在循环2中添加第三个循环,而不是让它按预期工作:

$media_args = array(
    \'post_type\' => \'attachment\',
    \'posts_per_page\' => -1,
    \'tax_query\' => array(
        array(
            \'taxonomy\' => \'products\',
            \'field\' => \'term_id\',
            \'terms\' => $product_tag
                )
            )
        );

// Loop 1 - To get the images with the desired tags

$media_items = new WP_Query($media_args); 

if ($media_items->have_posts()) {
    $parent = array();
    while ($media_items->have_posts()) : $media_items->the_post();

        // getting the post_parent and cleaning to weed out duplicates

        $parent[] = get_post_field(\'post_parent\', $post->ID);
        $parent_clean = array_unique($parent);
    endwhile;
}

$product_post_args = array(
    \'post_type\' => \'post\',
    \'posts_per_page\' => -1,
    \'post__in\' => $parent_clean
);

// Loop 2 - Get the posts which have the images from Loop 1 as attachments without duplicating the same post

$product_posts = new WP_Query($product_post_args);

if ($product_posts->have_posts()) {
   while ($product_posts->have_posts()) : $product_posts->the_post();

       $product_image_args = array(
           \'post_type\' => \'attachment\',
           \'posts_per_page\' => 1,
           \'post_parent\' => $post->ID,
           \'tax_query\' => array(
               array(
                   \'taxonomy\' => \'productcats\',
                   \'field\' => \'term_id\',
                   \'terms\' => $product_tag
                   )
               )
       );

       // Loop 3 - To get the first attachment with the desired tag for each post

       $product_images = new WP_Query($product_image_args);

       while ($product_images->have_posts()) : $product_images->the_post();               
           $img = wp_get_attachment_image_src( $post->ID, \'browse\' );
           echo $img[0];
       endwhile;

  endwhile;
} 

结束

相关推荐

The Loop in Static Page

我对环路有一些问题。我以“Twenty14”主题为例。我正在使用基本循环创建2个php文件。一个是家。其中一个是名为示例页的模板页。php。两者都包含此代码;if( have_posts() ) : while( have_posts() ) : the_post(); the_content; endwhile; endif; 没什么特别的,唯一的区别是我在示例页面上有模板声明。php/** * Template Nam