• Resolved itsonlybarney

    (@itsonlybarney)


    I have created a Custom Post Type, and modified the archives loop to output just the link to the Custom Post.

    Is it possible to count the number of items extracted from the database using the loop?

    This is my loop query:
    $loop = new WP_Query( array( 'post_type' => 'cclub', 'orderby' => 'title', 'order' => 'ASC' ) );

    I thought I could count the items using the count() function from PHP using the follow:
    $count = count($loop);

    But alas the answer given was 1.

Viewing 6 replies - 1 through 6 (of 6 total)
  • esmi

    (@esmi)

    Why not just set up a simple variable and increment it at the start of each loop pass?

    Thread Starter itsonlybarney

    (@itsonlybarney)

    If I wanted to have two columns, and the items listed in alphabetical order, I want the columns to be like:

    A | E
    B | F
    C | G
    D | H

    rather than:

    A | B
    C | D
    E | F
    G | H

    This is assuming my understanding of what you are saying is correct.

    esmi

    (@esmi)

    So when $count <5, display the posts in the left column.
    When $c > 4 && $c < 11, display posts in the left column.
    When $c > 10, $c = 1

    Thread Starter itsonlybarney

    (@itsonlybarney)

    When I ran the following code:
    $count = count($loop);

    $count was always equal to 1, no matter how many items the loop pulled out of the DB.

    Michael

    (@alchymyth)

    try:

    $count = $loop->post_count;

    Thread Starter itsonlybarney

    (@itsonlybarney)

    Thanks alchymyth, your code worked perfectly

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Count Items in Loop’ is closed to new replies.