2012-08-01 71 views
1

我希望有人可以協助,因爲這是在殺我。我正在和別人的代碼一起工作,遇到很多麻煩,而且我只是在wordpress上發現了一些新東西。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; 

}

回答

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); 

witin的functions.php文件或包含的functions.php中的文件包括...

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 
+0

所以,我只是用示例代替舊代碼喲你提供了嗎?那些首先引用大圖和小圖的人會發生什麼?實際上在小圖像和下面的所有其他圖像之間存在一些內容,我在代碼示例中忽略了這些內容以保持簡單。對不起,我還沒有太熟悉WordPress。 – Glyph 2012-08-02 00:31:09

+0

另外,有沒有更簡單的方法。例如,我可以看到第二個圖像可以使用:$ images [2] 有沒有辦法讓這個範圍?即.. $ images [2-100] – Glyph 2012-08-02 01:53:14

+0

爲我提供了該頁面的完整腳本,以及您希望爲我進一步提供幫助的示例 – Mwayi 2012-08-02 08:22:16

相關問題