2014-03-24 3 views
0

привет у меня есть этот код я создал в контент-gallery.php , но когда я использую Printf, чтобы показать количество фотографий в течение поста не показываетWordPress функции пост галерея не отображается количество сообщений

он должен быть напечатан [Эта публикация содержит 0 фото.] но не работает. Зачем? когда мои ошибки?

<?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.

Смежные вопросы