WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Get number of posts in a cat (8 posts)

  1. xrun
    Member
    Posted 2 years ago #

    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?

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

  3. xrun
    Member
    Posted 2 years ago #

    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?

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

    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/

  5. xrun
    Member
    Posted 2 years ago #

    Good. Thanks. :)

  6. xrun
    Member
    Posted 2 years ago #

    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.

  7. xrun
    Member
    Posted 2 years ago #

    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.

  8. xrun
    Member
    Posted 2 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic