0

I had this code working as I needed it to...but now I need to add a post count where the "____" is, beside "posters", so I can manipulate the css depending on how many posters are showing, for example: <div id="posters1"> or <div id="posters2"> or <div id="posters3">, etc.

I believe this can be done with $post_count or $found_posts but I'm not sure where to place it and or where to move the posters div (ie: inside the loop?):

<div id="posters_ _ _ _">
    <?php 
        $movieNumber = 1;
        $today = date('Ymd');
        $posts = array (
            'post_type' => 'movie',
            'posts_per_page'   => -1,
        );
        $loop = new WP_Query( $posts );
        while ( $loop->have_posts() ) : $loop->the_post();

            $attachment_id=get_field('movie_poster',$post->post_id);
            $size = "poster"; // (thumbnail, medium, large, full or custom size) 
            $image = wp_get_attachment_image_src( $attachment_id, $size ); // url = $image[0], width = $image[1], height = $image[2];

            echo '<div class="poster"><a class="various" rel="movies" href="#fancy' . $movieNumber++ . '"><img src="' . $image[0] . '" /></a></div><!-- #poster -->';

        endwhile; 
    ?>
</div><!-- #posters -->

3 Answers 3

2

rearrange your code and use post_count; example:

    <?php 
        $movieNumber = 1;
        $today = date('Ymd');
        $posts = array (
            'post_type' => 'movie',
            'posts_per_page'   => -1,
        );
        $loop = new WP_Query( $posts ); ?>
    <div id="posters<?php echo $loop->post_count; ?>">
    <?php while ( $loop->have_posts() ) : $loop->the_post();
...etc...
1
  • Perfect. Exactly what I was asking. I didn't know the $loop->post_count part or where to put the div.
    – codeview
    Commented Jul 8, 2014 at 2:44
0

Before your you have to create a new query then count the results:

global $wpdb;
$count = $wpdb->get_var("SELECT COUNT(ID) FROM $wpdb->posts WHERE post_type='movie'");
<div id="posters<?php echo $count; ?>">
5
  • I'm not getting any # output with that code, just <div id="posters">
    – codeview
    Commented Jul 4, 2014 at 20:59
  • Hey, sorry. i remembered that you have to replace 'post_id' to 'ID' i edited it in my answer.
    – MEDZ
    Commented Jul 4, 2014 at 21:02
  • That works now. So, I have 5 posts total right now...but if I change my posts_per_page to "3" instead of "-1", the $count is still the number of posts (5). I need the $count to be limited to the number of posts being displayed, no the total number of posts.
    – codeview
    Commented Jul 4, 2014 at 21:27
  • you can add 'LIMIT 3' at the end of the query but it will not be dynamic and i don't believe there is another way since you change it from the inside and want to affect the outside.
    – MEDZ
    Commented Jul 4, 2014 at 21:35
  • right, that was my original question: how to make it so it's all together (no inside vs. outside).
    – codeview
    Commented Jul 4, 2014 at 21:39
-1

Simply declare a variable above like $i=0; and use $i++ in while loop

echo $i; where you want to show number of posts displaying.

may it will work

1
  • yes, you can do it this way! Just append the html in a variable if you need counter to show before it and add html after it Commented Jul 5, 2014 at 6:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.