• Resolved xencinas

    (@xencinas)


    Hello,

    I am trying desperately to find the way to count the number of post displayed on a page. If I count the published it works of course but I don’t need that.

    I want to count the number of post displayed on the page let’s say 3 (and in the WP settings I only show 3 posts per page), in order to hide a title like “timeline” before next_post_link(). So if on a result page I only have 1 result the title “timeline” won’t show.

    hope I am clear enough… Thanks for your help!

    Xavier

Viewing 8 replies - 1 through 8 (of 8 total)
  • So… you just want to count them? Like display the number?

    <?php
    $count = 1;
    if (have_posts()) : while(have_posts()): the_post(); ?>
    <?php echo $count; ?>
    layout of template file here
    <?php $count++;
          endwhile; endif; ?>

    that should echo the count for each post on the page, starting with “1”.

    Thread Starter xencinas

    (@xencinas)

    Actually I want to count the number of post on the page so if this number is bigger than 3 (for example), I won’t show a text (a title for instance).

    To be more specific. I set up on the admin that I want to display 3 post on a page (index for example). Then I have a navigation like next_post_link() and a title that go with like “navigate”. If there is 3 or less post on the page I want to be able to hide this text so it won’t look: the title and then nothing to clic after.

    Hope i am clear. Thanks for your help and this beginning of answer ??

    Ah – so you want to do something *with* that count then ?? Well, that’s pretty simple. It still uses the above code – but instead of echoing out the $count, then do what it is you need to do using $count as a flag.

    <?php
    $count = 1;
    if (have_posts()) : while(have_posts()): the_post(); ?>
    layout of regular template file here
    <?php $count++;
          endwhile; endif; ?>
    <?php if ($count > '3') { ?>
    show the link and title here (because it's more than 3)
    <?php } ?>

    That *might* work. Untested – you might have to play with it a little.

    @doodlebee,
    You has shown a solution. But actually we want to know is there a way to print the number of post per page in a particular category so that we can use this information to perform complex calculation.
    If this information is available many loops can be manipulated and created easily.

    What about:

    <?php
    echo 'Count of posts array ' . count($posts);
    ?>

    This may also help..

    <?php
    if($wp_query->found_posts <= get_option('posts_per_page') && (!get_query_var('paged') || get_query_var('paged') < 2)) {
     // If the total amount of posts in the query is "less than" or "equal to" the amount to show per page and it's the first page
    }
    else {
     // Else there's more results on additional pages
    }
    ?>

    As alternative.. ??

    @michaelh
    Worked like charm buddy.
    Thanks a lot

    <?php
    $count = 1;
    if (have_posts()) : while(have_posts()): the_post(); ?>
    layout of regular template file here
    <?php $count++;
          endwhile; endif; ?>
    
    <?php if((preg_match('/\/2\//',$_SERVER['REQUEST_URI']))) {
     $count = 51;
    } elseif((preg_match('/\/3\//',$_SERVER['REQUEST_URI']))) {
     $count = 101;
    } else {
     $count = 1;
    } ?>

    Can anyone tell me how to tell this to keep track of the count once it hits page 2,3,etc..? Looking around, I was able to find this but it doesn’t work..

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Count the number of posts displayed on a page’ is closed to new replies.