Support » Themes and Templates » Get number of posts in a cat

  • Resolved xrun

    (@xrun)


    Hi.
    I’d like to add code to a page which checks and displays a number for how many posts there are in a given category (either by cat name or id number).
    Any pointers?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Thread Starter xrun

    (@xrun)

    Excellent. Can this be included in the page as written in the dashboard, or do I need to install a plugin to allow php in a post/page?

    if you want to use the php code in the post/page content, you need to install and activate a plugin; for instance http://wordpress.org/extend/plugins/exec-php/

    Thread Starter xrun

    (@xrun)

    Good. Thanks. 🙂

    Thread Starter xrun

    (@xrun)

    Seems like I spoke too soon. I need to display a link if the number of posts in the given category is higher than a given integer.
    I was trying to work it out based on the code samples, but I’m not getting it. When trying to display the number of posts by echoing the count I only get the word “count” echoed on the page.

    Thread Starter xrun

    (@xrun)

    Here’s what I got so far, using a slightly different function. This correctly outputs a list of category names, limits the list to category 7, and shows the number of posts in it.
    The ‘&echo’ part may be redundant, but was put in there while experimenting cause it’ll just hide the entire output.

    $count = wp_list_categories('count=1&include=7&echo=1');

    However, I dont need to actually display the name of the category or the number. What I’m working towards is showing a link depending on the value of the post count. If the count is too low the link should not be displayed.

    Edit:
    I need to apologize, your code does exactly what I was asking for. Now I just need to determine if the link should be shown or not.

    Thread Starter xrun

    (@xrun)

    And completing this:

    <?php $count = get_category(7)->count ;
    if($count>8) echo('<a href="'.get_bloginfo('url').'/?page_id=9999"> some page </a>' );
    ?>

    Where the number 9999 represents some page on the same site, your page may differ.
    Tested, this displays a link to a given page if the number of posts exceeds 8 in category 7. Alternately if the link is to be shown only when the number of posts is in a given range:

    <?php $count = get_category(7)->count ;
    if($count>8 && $count<16) echo('<a href="'.get_bloginfo('url').'/?page_id=9999"> some page </a>' );
    ?>

    Which shows the link only when the number of posts in category 7 is between 8 and 16 (not 8 or 16 exactly).

    Thanks to alchymyth for pointing me in the right direction.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Get number of posts in a cat’ is closed to new replies.