我有一个图库附在一页上。在该页面上,我正在运行以下查询:
$events_gallery = new WP_Query( // Start a new query for our videos
array(
\'post_parent\' => $post->ID, // Get data from the current post
\'post_type\' => \'attachment\', // Only bring back attachments
\'post_mime_type\' => \'image\', // Only bring back attachments that are images
\'posts_per_page\' => \'3\', // Show us the first three results
\'status\' => \'inherit\', // Inherit the status of the parent post
\'orderby\' => \'rand\', // Order the attachments randomly
)
);
我尝试了很多方法,但由于某种原因,我无法让附件返回。我是不是遗漏了什么?
Update*
感谢Wok为我指明了正确的方向。
原来我用的是“status”而不是“post\\u status”。食品法典委员会在对“附件”帖子类型的上下文解释中使用了“状态”作为示例。我更新了法典,改为引用“post\\u状态”。正确的代码如下:
$events_gallery = new WP_Query( // Start a new query for our videos
array(
\'post_parent\' => $post->ID, // Get data from the current post
\'post_type\' => \'attachment\', // Only bring back attachments
\'post_mime_type\' => \'image\', // Only bring back attachments that are images
\'posts_per_page\' => \'3\', // Show us the first three results
\'post_status\' => \'inherit\', // Attachments default to "inherit", rather than published. Use "inherit" or "any".
\'orderby\' => \'rand\', // Order the attachments randomly
)
);
SO网友:Chad Von Lind
我已经能够使用此代码显示帖子附件中的所有图像。
<?php
$args = array( \'post_type\' => \'attachment\', \'orderby\' => \'menu_order\', \'order\' => \'ASC\', \'post_mime_type\' => \'image\' ,\'post_status\' => null, \'post_parent\' => $post->ID );
$attachments = get_posts($args);
if ($attachments) {
foreach ( $attachments as $attachment ) { ?>
<img src="<?php echo wp_get_attachment_url( $attachment->ID , false ); ?>" />
<?php }
} ?>
要回显原始全尺寸图像的URL,可以将该图像链接到
<?php echo wp_get_attachment_url( $attachment->ID , false ); ?>
希望这是一种实现您想要做的事情的方法。