WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Count Items in Loop (7 posts)

  1. itsonlybarney
    Member
    Posted 2 years ago #

    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.

  2. esmi
    Forum Moderator
    Posted 2 years ago #

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

  3. itsonlybarney
    Member
    Posted 2 years ago #

    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.

  4. esmi
    Forum Moderator
    Posted 2 years ago #

    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

  5. itsonlybarney
    Member
    Posted 2 years ago #

    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.

  6. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    try:

    $count = $loop->post_count;

  7. itsonlybarney
    Member
    Posted 2 years ago #

    Thanks alchymyth, your code worked perfectly

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.