2014-03-24 72 views
0

的數量您好,我有這樣的代碼我在內容gallery.php 創建但是當我用printf顯示的照片在帖子內的數量不顯示它WordPress郵寄畫廊功能不顯示職位

它必須打印 [此帖子包含9張照片。] 但不起作用。爲什麼? 當我的錯誤?

<?php 

$the_images = get_children(array(
    'post_parent' => $post->ID, // Pass the ID of a post or Page to get its children. Pass 0 to get attachments without parent. Pass null to get any child regardless of parent. 
    'post_mime_type'=> 'image', // A full or partial mime-type, e.g. image, video, video/mp4, which is matched against a post's post_mime_type field. 
    'post_type'  => 'attachment', 
    'orderby'  => 'menu_order', 
    'order'   => 'ASC', 
    'numberposts' => 999 
)); 

if (!empty($the_images)) { 
    $the_total_images = count($the_images); 
    $the_images  = array_slice($the_images, 0, 10); // prin_r form 0 to 10 images 
?> 

<p><strong> 
    <?php printf (_n('This Post Gallery Contains %s Photo.', 'This Post Gallery Contains %s Photos.', $the_total_images, 'the-bootstrap'), $the_total_images); ?> 
</strong></p> 

<?php } ?> 
+0

你試過使用'sprintf()'而不是'printf()'? – Cyclonecode

回答

0

嘗試使用的sprintf()代替printf()

另外,使用功能時請確保沒有空格。一旦你在括號內,空格是可以的,但是在printf()中有空格是不好的做法,大多數時間錯誤或不起作用。

0

您正在檢查the_images是否爲空。如果它是空的,那麼$the_total_images從不設置。

我不明白你對array_slice的使用。肯定只有10個帖子會更有效率。

以下是我會做:

<?php 
global $post; 

$the_images = get_posts(array(
    'post_parent' => $post->ID, 
    'post_mime_type' => 'image', 
    'post_type'  => 'attachment', 
    'post_status' => 'any', 
    'orderby'  => 'menu_order', 
    'order'   => 'ASC', 
    'posts_per_page' => -1, 
)); 

$the_total_images = ($the_images) ? count($the_images) : 0; 
$image_count_text = sprintf(_n('This Post Gallery Contains %s Photo.', 'This Post Gallery Contains %s Photos.', $the_total_images, 'the-bootstrap'), $the_total_images); ?> 

<p><strong><?php echo $image_count_text; ?></strong></p> 

$the_total_images不再依賴於get_posts()結果。我已經離開它加載所有的圖像附件,但我認爲如果你只打算使用更好的解決方案可以找到10.