Wordpress帖子页面未显示图库中的所有图像

时间:2012-08-01 14:16:16

标签: wordpress gallery

我希望有人可以提供帮助,因为这会杀了我。我正在使用别人的代码并且遇到很多麻烦而且我只是wordpress的新手。

我想要实现的是帖子从页面顶部的图库中显示第一个图像,然后是它下面的第二个图像(在某些文本内容旁边),然后在那之下我希望它显示全部画廊中的剩余图像。出于某种原因,它目前将画廊中的最后两张图片留下,好像它们被故意排除在外。非常感谢你的帮助,我很感激。

这是当前的代码:

<?php

get_header();

if (have_posts()) {
while (have_posts()) {
    the_post();
    $images = GetPostImages(); // Get Post Images
    $tags = GetPostTags(); // Get Post Tags String
    ?>                    
<div class="project">

<img src="<?php echo $images[1]->guid; ?>" class="large" width="1000" height="700" />

<div class="projectdesc">  
    <h1><?php the_title(); ?></h1>
        <p class="tags"><?php echo $tags; ?></p>

    <?php 
the_content(); ?>

</div>

<img src="<?php echo $images[2]->guid; ?>" class="right" width="600" height="400" />

 <?php
        // Additional Project Images
        for ($i = 3; $i < count($images); $i++) {
            ?>
            <img src="<?php echo $images[$i]->guid; ?>" class="large" width="1000"/>        
            <?php
        }
        ?> 

    </div>

    <?php
}
}

get_footer();
?>`

functions.php文件中的代码也在下面。我认为这可以控制帖子页面处理图像的方式吗?

function GetPostImages() {
global $post;

// Get image attachments
$images = get_children('post_type=attachment&post_mime_type=image&post_parent='.$post->ID);

// Sort by menu_order
foreach ($images as $key=>$image) {
    $newImages[$image->menu_order] = $image;
}

// Return
return $newImages;

}

1 个答案:

答案 0 :(得分:2)

使用 wp_get_attachment_image_src()获取图片来源

$attr = wp_get_attachment_image_src(int $id,string $size); //where $id is image post id

$width  =  $attr[1];
$height =  $attr[2];
$src    =  $attr[0]; //the source

echo sprintf('<img src="%s" alt="" height="%s" width="%s"/>',$src,$height,$width);

在functions.php文件中或functions.php中的include文件包含...

function setup_images(){

//Register Image dimensions
add_image_size( 'large',1000,700,true); //'large' will be the string used in $size
add_image_size( 'med',600,400,true);  //or 'med'
}
add_action('after_setup_theme','setup_images'); //hook that function up in the right place
相关问题