Wordpress: - 拉动所有附件图像,包括特色图像

时间:2011-12-06 18:05:32

标签: wordpress

我在拉动包括图像在内的所有附件时遇到了真正的困难......事情是,我需要能够控制内容的显示方式......

e.g。

<a href="(Path-to-full-image)" rel="customrel" class="aclasstodeine">
<img src="thethumbnailsize(defined in functions.php)" alt=""
class="aclasstodefine" />
</a>

真希望有人可以提供帮助

2 个答案:

答案 0 :(得分:1)

要获取所有附件图像,您需要进行查询。

$args = array(
    'post_type'      => 'attachment',
    'post_parent'    => $post->ID,
    'post_status'    => 'inherit',
    'numberposts'    => -1
);

$images = get_posts($args);

这会将附加到帖子的任何图像加载到$ images变量中,然后对它们执行foreach循环以获取图像:

<?php foreach ($images as $i) : ?>
    <a href="<?php wp_get_attachment_image_src($i->ID, 'full'); ?>" rel="customrel" class="aclasstodeine">
    <img src="<?php wp_get_attachment_image_src($i->ID, 'thumbnail'); ?>" alt="" class="aclasstodefine" />
    </a>
<?php endforeach; ?>

您可以在此处阅读wp_get_attachment_image_src:http://codex.wordpress.org/Function_Reference/wp_get_attachment_image_src

答案 1 :(得分:0)

更有可能是这样的:

$args = array(
    'post_type'      => 'attachment',
    'post_parent'    => $post->ID,
    'post_status'    => 'inherit',
    'numberposts'    => 1//-1
);
$images = get_posts($args);

foreach ($images as $i) :
    $poza_thumb = wp_get_attachment_image_src($i->ID, 'thumbnail');
    //$poza_fullsize = wp_get_attachment_image_src($i->ID, 'full');
    //print_r($poza_thumb);//you will see that $poza_thumb is an array containing url, width, height
    ?>
    <img src="<?php echo $poza_thumb[0];?>" width="<?php echo $poza_thumb[1];?>" height="<?php echo $poza_thumb[2];?>" rel="customrel" class="aclasstodeine"/>

<?php
endforeach;