Number of posts in categories

Number of posts in categories

Postby einfachsaufen.de on Wed Nov 05, 2008 3:11 pm

Hi, i want to show on index the number of all posts at all categories and the number of categories like:
xxx posts in xxx categories in this blog
You are an idea how i can realize it? It's easy with variables but I found nothing for this.
blogID=1
einfachsaufen.de
 
Posts: 19
Joined: Wed Nov 05, 2008 1:33 pm
LifeType Version: 1.2.8

Re: Number of posts in categories

Postby jondaley on Thu Nov 06, 2008 7:49 am

I am not sure if the numbers already exist, but you can use the "standard" foreach loop that shows the categories (usually on the sidebar of most templates) and how many posts there are in each category, and add a couple of counters to count what you want.
Something like:

Code: Select all
{assign var=numCategories value=0}
{assign var=numPosts value=0}
{foreach from=$articlecategories item=panel_articleCategory}
  {assign var=numPosts value=$numPosts+$panel_articleCategory->getNumArticles()}
  {assign var=numCategories value=$numCategories+1}
{/foreach}
There are {$numPosts} posts in {$numCategories} categories.


You might need to split the first assign statement into two parts, sometimes smarty doesn't like the formulas being too complicated.

Got to run - that code may or may not work perfectly
jondaley
Lifetype Expert
 
Posts: 6169
Joined: Thu May 20, 2004 6:19 pm
Location: Pittsburgh, PA, USA
LifeType Version: 1.2.11 devel branch

Re: Number of posts in categories

Postby einfachsaufen.de on Thu Nov 06, 2008 9:50 am

it doesn't work.

I create:
Code: Select all
{foreach from=$articlecategories item=articleCategory}
{$articleCategory->getNumArticles()}
{/foreach}

See: http://einfachsaufen.de/system/index.ph ... ow=sitemap
So it shows the Numbers. How I can summate them?
einfachsaufen.de
 
Posts: 19
Joined: Wed Nov 05, 2008 1:33 pm
LifeType Version: 1.2.8

Re: Number of posts in categories

Postby jondaley on Thu Nov 06, 2008 4:02 pm

ugh, smarty cares about spaces. Simply remove the spaces from around the + sign and it will work. (I've edited my above code, so you can simply grab the code again)

And a spectacularly poor error message to:
Smarty error: [in ./templates//jondaley/panel.template line 27]: syntax error: invalid attribute name: '+' (Smarty_Compiler.class.php, line 1547) in /home/jondaley/www/plog/class/template/smarty/Smarty.class.php on line 1095
jondaley
Lifetype Expert
 
Posts: 6169
Joined: Thu May 20, 2004 6:19 pm
Location: Pittsburgh, PA, USA
LifeType Version: 1.2.11 devel branch

Re: Number of posts in categories

Postby einfachsaufen.de on Thu Nov 06, 2008 5:55 pm

thats not very helply..
einfachsaufen.de
 
Posts: 19
Joined: Wed Nov 05, 2008 1:33 pm
LifeType Version: 1.2.8

Re: Number of posts in categories

Postby jondaley on Fri Nov 07, 2008 12:32 am

Huh? Did you read my post? Get the code from my above post now, and it will work. I tried it out on my own blog, and it works fine.
jondaley
Lifetype Expert
 
Posts: 6169
Joined: Thu May 20, 2004 6:19 pm
Location: Pittsburgh, PA, USA
LifeType Version: 1.2.11 devel branch

Re: Number of posts in categories

Postby einfachsaufen.de on Fri Nov 07, 2008 8:04 am

I did it. So, now it shows:
There are 152 posts in 153 categories
But i have just only 15 categories. Whats up? see: http://einfachsaufen.de/system/index.ph ... ow=sitemap
Another Problem is the value. I've only 117 Posts but it shows 152. So, i have much posts in two, three or four categories but it is the same post. If you summate them so, 152 is the value but its not my wish :-(
einfachsaufen.de
 
Posts: 19
Joined: Wed Nov 05, 2008 1:33 pm
LifeType Version: 1.2.8

Re: Number of posts in categories

Postby jondaley on Fri Nov 07, 2008 9:59 pm

the category error was a simply typo - I didn't copy and paste all of the code back correctly.
That has been fixed.
The articles in multiple categories is more difficult. Maybe there is an easier way, I'm not sure.
jondaley
Lifetype Expert
 
Posts: 6169
Joined: Thu May 20, 2004 6:19 pm
Location: Pittsburgh, PA, USA
LifeType Version: 1.2.11 devel branch

Re: Number of posts in categories

Postby einfachsaufen.de on Sat Nov 08, 2008 12:54 pm

First, thank you for this code.
But you have an ideo how i can show the posts in multiple categories correctly?
einfachsaufen.de
 
Posts: 19
Joined: Wed Nov 05, 2008 1:33 pm
LifeType Version: 1.2.8

Re: Number of posts in categories

Postby jondaley on Sat Nov 08, 2008 4:02 pm

I am not sure if there is an easy way to do it. It is a trivial plugin, but I haven't thought of a way to do it just from template code.
jondaley
Lifetype Expert
 
Posts: 6169
Joined: Thu May 20, 2004 6:19 pm
Location: Pittsburgh, PA, USA
LifeType Version: 1.2.11 devel branch


Return to Everything Else Goes Here!

cron