Display number of words in article at top of article?

For Joomla! 3.x Coding related discussions, you could also use: http://groups.google.com/group/joomla-dev-general

Moderators: ooffick, General Support Moderators

Forum rules
Locked
emeyer
Joomla! Explorer
Joomla! Explorer
Posts: 352
Joined: Thu Sep 29, 2005 2:37 am

Display number of words in article at top of article?

Post by emeyer » Sun Feb 07, 2016 7:00 am

Any ideas on the best way to do this? I was thinking of adding a php to the template like this, but maybe you have a better idea. I started with this, but words without spaces between them and html tags didn't get counted:

Code: Select all

$html.='<dt>Word count:</dt><dd>'.str_word_count(strip_tags($this->item->fulltext)).'</dd>';
So now I have this:

Code: Select all

$wcount = preg_replace('#<[^>]+>#', ' ', $this->item->fulltext);
$wcount = str_replace('&nbsp;',"",$wcount);
$html.='<dt>Word count:</dt><dd>'.str_word_count($wcount).'</dd>';

User avatar
Per Yngve Berg
Joomla! Master
Joomla! Master
Posts: 30930
Joined: Mon Oct 27, 2008 9:27 pm
Location: Romerike, Norway

Re: Display number of words in article at top of article?

Post by Per Yngve Berg » Sun Feb 07, 2016 12:37 pm

The proper way to do it is to write a Plugin.

https://docs.joomla.org/Plugin

emeyer
Joomla! Explorer
Joomla! Explorer
Posts: 352
Joined: Thu Sep 29, 2005 2:37 am

Re: Display number of words in article at top of article?

Post by emeyer » Sun Feb 07, 2016 1:05 pm

Well, it doesn't actually work for my site design, because I wanted it in the article description next to the author, category, date created, etc. I put it the above in the template, and it does seem to work fine.

User avatar
sudo-web
Joomla! Ace
Joomla! Ace
Posts: 1325
Joined: Fri Jan 22, 2016 7:10 pm
Location: Vienna - Austria
Contact:

Re: Display number of words in article at top of article?

Post by sudo-web » Fri Feb 12, 2016 7:36 pm

Because you want to have it on the article you can’t use the Template index.php file. As Per Yngve Berg said the proper way is a plugin.

It’s not so hard. You have to know that is the right event for your plugin. In your case you can use onContentPrepare. You only have to check if the call is coming from com_article and what type of view is called because I am not sure if you like to display it on every view.

Than just count the fulltext as you did and add the result properly.

Here some info about creating a plugin: https://docs.joomla.org/J3.x:Creating_a ... for_Joomla and some more information about the plugin events: https://docs.joomla.org/Plugin/Events
Visit me on my Webdesign Webpage: https://www.posit.at

emeyer
Joomla! Explorer
Joomla! Explorer
Posts: 352
Joined: Thu Sep 29, 2005 2:37 am

Re: Display number of words in article at top of article?

Post by emeyer » Sat Feb 13, 2016 9:25 pm

But I don't need a plugin. I already made template overrides for all my content. I just needed to add four lines of PHOP to the template override. It's done.

User avatar
sudo-web
Joomla! Ace
Joomla! Ace
Posts: 1325
Joined: Fri Jan 22, 2016 7:10 pm
Location: Vienna - Austria
Contact:

Re: Display number of words in article at top of article?

Post by sudo-web » Sat Feb 13, 2016 9:36 pm

Of course there many ways for a single problem, all we say is that this would be the prober way. I’m not sure is this issue solved for you, or do you have a concert question?
Visit me on my Webdesign Webpage: https://www.posit.at

emeyer
Joomla! Explorer
Joomla! Explorer
Posts: 352
Joined: Thu Sep 29, 2005 2:37 am

Re: Display number of words in article at top of article?

Post by emeyer » Sat Feb 13, 2016 9:44 pm

Well, it seems to me the default options for article information display on blog and article pages haSn't be redesigned in 20 years. It would be simple to add these options, and a plugin can't insert them up there without template overrides. I did not ice there is no plugin for it though, so I guess most people dont care, considering how many zillions there are of the things these days.


Locked

Return to “Joomla! 3.x Coding”