从帖子中获取附件的URL?

时间:2013-03-20 作者:osos

我使用以下代码将图像附加到帖子

    $current_id = get_the_ID();
    $rows = $wpdb->get_results("select guid from $wpdb->posts where post_parent = $current_id");

    foreach ($rows as $row) {

    ?>

        <a href="<?php echo $row->guid; ?>" data-titan-lightbox="on" data-titan-group="<?php the_ID() ?>"  style="display: none"><img src="<?php echo $row->guid; ?>" alt="Caption" class="Thumbnail thumbnail " width="228" height="160"></a>

    <?php   
但这段代码只提供包含图像的文件夹的图像路径。

如何仅从该循环中获取图像?

1 个回复
SO网友:jfacemyer

我认为你做得不对。

您可以使用get_posts 要获取当前帖子的附件,请执行以下操作:

$args = array( \'post_type\' => \'attachment\', \'numberposts\' => -1, \'post_status\' =>\'any\', \'post_parent\' => $post->ID ); 
$attachments = get_posts($args);
if ($attachments) {
    foreach ( $attachments as $attachment ) {
        echo apply_filters( \'the_title\' , $attachment->post_title );
        the_attachment_link( $attachment->ID , false );
    }
}
请参见http://codex.wordpress.org/Template_Tags/get_posts#Show_attachments_for_the_current_post

结束