Show Category Count Help With PHP Please?
-
I’ve tried plugins that showed category counts but they slowed the sidebar loading a lot.
I’ve tried a simple
<?php wp_list_categories('show_count=1'); ?>
and various other suggestions such as<?php $postcounts = $wpdb->get_col("SELECT category_count FROM $wpdb->categories WHERE cat_ID=1 and category_count != 0"); if ( ! empty($postcounts) ) { foreach ($postcounts as $postcount) { echo $postcount; }} ?>I’m putting the code into archive.php in my theme (tried sidebar first).
I’ve also wrapped it in a div with and without an id, tried adding ul and li…
Nothing’s working. What am I doing wrong?
-
What’s wrong with using the template tag, wp_list_categories(), or even the Categories widget?
I say that, because with 2.3 the whole category thing gets moved into the terms table.
But that syntax for wp_list_categories seems okay to me.
Hi Michael,
I did originally use a categories widget, but for some reason it slowed down the loading of the sidebar a lot. That’s why I’m trying to add a simple ‘display count’ instead.
If the syntax is ok, which I’ve checked over and over, then I’m stuck on why I can’t get it to work.
If it didn’t work in the sidebar.php because it’s widgetized, then why won’t it work in the archive.php? Should I be putting it somewhere else?Vera
Your code — and it is correct for WP 2.2.x — will, if a value exists in ‘category_count’ for category #1 (i.e.
cat_ID=1), print out that number.So questions for us to ask: Are there actually posts in category 1 on your blog? Is this really what you want as a result?
Hm? I posted a reply 20 min ago and it’s not here.
I have no category #1. It was deleted at some point when I was reorganizing. I’ve categories #2 through #26 (#2 is the blogroll/links category).
What I want to accomplish is to show the number of posts in each category. (I’m not sure what printing out the number means.)
I thought that 1 in this case was ‘true’ and 0 was ‘false’. Is that incorrect?
Vera
“I thought that 1 in this case meant ‘true’ and 0 meant ‘false’. This is wrong?“
Yep. The cat_ID record represents the # for each category.
You can change it to:
<?php $postcounts = $wpdb->get_col("SELECT category_count FROM $wpdb->categories WHERE category_count != 0"); if ( ! empty($postcounts) ) { foreach ($postcounts as $postcount) { echo $postcount; }} ?>However, this will just print a long string of numbers: the count values without any spaces. Something tells me this also not quite what you want. And even inserting spaces or some other separator, there’ll be no context to the numbers, that is nothing tying count to category.
This will get you closer to something useful:
<?php $categories = $wpdb->get_results("SELECT category_count, cat_name FROM $wpdb->categories WHERE category_count != 0"); if ( ! empty($categories) ) { foreach ($categories as $category) { echo "$category->cat_name($category->category_count) "; } } ?>Thank you Kafkaesqui. I have the true and false values reversed?
I replaced
<?php wp_list_categories('show_count=1'); ?>with the second block of code you posted, and it still isn’t showing the number of posts in each category.
(must stop hitting head against wall)
Should I go back and try it in the sidebar file rather than the archive.php or… ? Any further ideas very much appreciated.
Vera
“I have the true and false values reversed?“
1 does mean true and 0 does mean false, but only when we are talking about a boolean value. However, the cat_ID record in the categories table is the actual category ID # (your #2 through #26).
“I replaced … with the second block of code you posted, and it still isn’t showing the number of posts in each category.“
Are you sure, as I presently see this (after digging a bit) on your site:
Anecdotes(11 ) Blogging(22 ) Entrepreneurship(35 ) Ethics(12 ) Freedom(34 ) Happy(17 ) Internet Society(53 ) Life Hacks(8 ) Marketing(8 ) Medical System(4 ) Money(5 ) Open Source(4 ) Personal(41 ) Perspective(56 ) Plagiarism(4 ) Poetry(8 ) Rants(7 ) Real Estate(6 ) Sad(4 ) Semantic Web(9 ) Television(4 ) Toronto(10 ) Web Development(56 ) WordPress(4 )
Edit note: The code should work just about anywhere (and in any template) on your blog.
Thank you for the explanation of the values. I very much appreciate that.
I could not see them. Cleared all caches. Went back to my blog in both FF and IE, and did not see those post counts. Then I went to a single category page, and saw the list of cat names and post counts displayed in an unstyled block above the sidebar.
I guess that I’m not explaining clearly what it is I am trying to achieve. Must be the 17 hour day.
I am simply trying to display the number of posts in each category, immediately following that category’s name, in the sidebar (which appears on every page).
Vera
Success. I now have post counts displayed for categories (and archives too). The addition that worked was this first one I’d tried (in sidebar.php):
<li><h2>Categories</h2> <ul> <?php wp_list_categories('show_count=1&title_li=<h2>Categories</h2>'); ?> </ul> </li>It wasn’t a code issue.
I’m finding that I have to replace files with cleaned ones constantly. Something in my host’s server configuration is resulting in whitespace at the ends of files, even though I’ve not touched them since originally uploading. In this case I replaced all the widget files.
I have tried all of the code solutions here in both the sidebar.php and archive.php files. None of them seem to work. I have two blogs in the same root directory.
http://www.dangerdom.com/blog/
http://www.dangerdom.com/365/The first one works by default. The exact same sidebar and archive files in the second one do not give any result, any suggestiongs?…..
I realize this post is a couple of months old but since I had the same problem, I figured I would add that as a newbie, I finally figured out that if you have the sidebar widget installed, you need to set the category count using the widget not modify the sidebar.php theme code. Just click on the configure icon to the right of the widget.
The topic ‘Show Category Count Help With PHP Please?’ is closed to new replies.