• I am having a small problem but was hoping maybe someone can help me with it. I am displaying the number of posts in a certain category within the header of my site. IE: Category Name (12).

    On the main index page and the search results page and the posts page its working PERFECTLY, but if I go into a category page for some reason the number blanks out. IE: Category Name ()

    This is the code I am using to display the number, maybe someone can point out why it wouldn’t show the number on category pages as well?

    Thank you in advance,
    LawMaker

    <?php
    $cat = get_query_var('cat');
    $categories=get_categories('include=5'.$cat);
    if ($categories) {
      foreach($categories as $category) {
        echo '' . $category->count;
      }
    }
    ?>

    Note: include=5 is the category ID.

Viewing 5 replies - 1 through 5 (of 5 total)
  • outside the category archive, this line does not return anything:
    $cat = get_query_var('cat');
    and therefore does not disturb this
    get_categories('include=5'.$cat);
    what this 'include=5'.$cat does is to concatenate 'include=5' and the result from the first line, which is the category id of your category archive; for instance 17 – so you get:
    get_categories('include=517');
    which mostly would not exist, and therefore will not return a count.

    where is this get_categories('include=5'.$cat); coming from?
    what is this line supposed to do?
    how is the variable $cat to interact with the number 5?
    is $cat needed at all?

    Thread Starter LawMaker

    (@lawmaker)

    Well to be perfectly honest. I found the code through google a while ago. Im not very good with PHP and I am learning as I go along so I am not really 100% sure what those lines do. All I know was I needed to change the #5 to be the ID of the category in which I want to show the number of posts inside that cateogory.

    That is really all I am attemping to do. If a certain category ID has 37 posts that I want the number 37 to be displayed.

    Please let me know if I can give you any more info, if I did not anwser your question here.

    Thank you in advance
    LawMaker

    from your description, it is not really clear to me, where you want to display what category;

    if you want to use the code in the loop for posts on the front page (index.php) or archive pages (archive.php) then you could for instance use:

    get_the_category()
    https://codex.www.ads-software.com/Function_Reference/get_the_category

    <?php
    $cats = get_the_category();
    
    foreach( $cats as $cat ) {
        echo '' . $cat->count;
    break; //limit output to one category
    
    }
    ?>

    if you want to display it at the top of a category archive, your code could stay as it is – just delete the number 5.

    Thread Starter LawMaker

    (@lawmaker)

    I know im doing a very poor job describing what im trying to do. Here is more of a visual:

    Category Name One (12 Posts)
    Category Name Two (4 Posts)
    Category Name Three (6 Posts)

    Where 12/4/ and 6 is above, I need to dynamically update when a new post is added to that category.

    Note: Category Name One, Category Name Two, and Category Three are hard coded text, I just need the number to be displayed dynamically. I got it working on all pages with my code except the specific category pages where IE: If we are on the category name two page it would show “Category Name Two ( Posts)”

    Thanks again for the help,
    LawMaker

    step 1:
    get the category id from the category name:
    $cat = get_cat_ID('Category Name');
    https://codex.www.ads-software.com/Function_Reference/get_cat_ID

    step 2:
    get category info from the categeory id:
    $category = get_category( $cat );
    https://codex.www.ads-software.com/Function_Reference/get_category

    step 3:
    output the category count:
    echo $category->count;

    in total:
    for each line of hardcoded text, add following code once (with the category name replaced with your own);

    <?php
    $cat = get_cat_ID('Category Name');
    $category = get_category( $cat );
    echo ' ' . $category->count;
    ?>
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Category Numbers’ is closed to new replies.