Different CSS style for Featured articles in a list

Need help with the Administration of your Joomla! 2.5 site? This is the spot for you.

Moderator: General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.
Forum Post Assistant - If you are serious about wanting help, you will use this tool to help you post.
Locked
elavd
Joomla! Apprentice
Joomla! Apprentice
Posts: 6
Joined: Thu Nov 04, 2010 6:54 pm

Different CSS style for Featured articles in a list

Post by elavd » Tue Nov 27, 2012 8:57 am

Hello,
I'd like some help in presenting the Featured Articles in a different way than the normal (non-featured) ones.

More specifically:
I have a menu that shows a list of articles from a specific category (called "News").
In this list, some articles are featured and some aren't.

My goal is to show this list in a way that the Featured articles have a different style (e.g. color) than the non-featured ones, so that can be easily distinguished.

For a example, I would like to make a list like this:

Title Date Author
1) News title 1 Date 1 Author name 1
2) News title 2 Date 2 Author name 2
3) News title 3 Date 3 Author name 3
4) News title 4 Date 4 Author name 4
5) News title 4 Date 5 Author name 5


Where the news #2 and #5 are the Featured ones.

Is this possible?

elavd
Joomla! Apprentice
Joomla! Apprentice
Posts: 6
Joined: Thu Nov 04, 2010 6:54 pm

Re: Different CSS style for Featured articles in a list

Post by elavd » Thu Feb 07, 2013 4:54 pm

Anyone? :(

User avatar
rcarey
Joomla! Explorer
Joomla! Explorer
Posts: 471
Joined: Sat Apr 25, 2009 9:20 pm
Location: Minnesota (USA)
Contact:

Re: Different CSS style for Featured articles in a list

Post by rcarey » Thu Feb 07, 2013 5:54 pm

I have a solution for you. This is a technique I use in my projects.

The key is to add a CSS class to the list item if the item is featured. That way you can use CSS to add the styling you want to titles of featured items - but of course, that styling will not be applied to non-featured items.

Assuming you want to apply this styling for the module that lists articles within a category, you will need to override that modules view file (or create an alternative layout for it). The file to override is
/modules/mod_articles_category/tmpl/default.php
You should place a copy of this file here
/templates/<my template>/html/mod_articles_category/default.php
Create the mod_articles_category directory if needed. Now this copied file should be overriding the original file.

Within this file, go to about line 86 (shortly after the "else" statement). replace the line

Code: Select all

<li>
with this

Code: Select all

<li <?php echo ($item->featured) ? 'class="featured-title"' : ''; ?> >
This does exactly what I stated for a goal - if the item is featured, we add a CSS class to the list item. You can name the class whatever you want, but I chose featured-title. Now you just add the styling as you want leveraging the presence of this selectively added CSS class. Keep in mind that you might need to create a CSS rule based upon multiple selectors, such as
li.featured-title a

The file mentioned handles grouping of items by their subcategories. If you are using that option, you will need to apply the change I stated on the other lines that add <li> to the HTML.

Let me add that creating an alternative layout is the approach to take in case you want some modules to behave this way, and others not to do so. All you need to do is copy the original default.php and give it a new name (like highlighted.php). This file can sit either directory mentioned above. Then apply the change to the new file. Once that is in place... If a module is to highlight featured items, select the alternative layout of "highlight." Modules left with the "default" layout will display using the default.php template.
Randy Carey -- as of 2023 I'm mostly retired in web development, but still engaged with a few Joomla projects through
Careytech Studios http://careytech.com custom development for tailored or value-added web solutions

elavd
Joomla! Apprentice
Joomla! Apprentice
Posts: 6
Joined: Thu Nov 04, 2010 6:54 pm

Re: Different CSS style for Featured articles in a list

Post by elavd » Fri Feb 08, 2013 9:25 am

Hello my friend and thank you very much for your detailed answer!

Unfortunately, although I did everything that you told me, I had no result...

I've created the folder "mod_articles_category" (because it didn't exist) and place the copy of default.php with the added code, but with no result.

My guess is that the page that lists the articles (from the specific Category named "News") is not handled by a module, so I have to change the code from another file than the

Code: Select all

/modules/mod_articles_category/tmpl/default.php
In order to help you point me to the correct file that needs changes I will explain this:

In the Main Menu of my site, I have a menu item that lists the articles of a Category (see the image attached below).

The articles are listed in a table form (with <td> tags, not <li>) so I thing that I need to change another file with a code similar than the one you wrote me.

But which file is this???
Can you please help?
You do not have the required permissions to view the files attached to this post.

User avatar
rcarey
Joomla! Explorer
Joomla! Explorer
Posts: 471
Joined: Sat Apr 25, 2009 9:20 pm
Location: Minnesota (USA)
Contact:

Re: Different CSS style for Featured articles in a list

Post by rcarey » Fri Feb 08, 2013 2:46 pm

You are right - since I was assuming you were referring to a module, I gave specific instructions for adding this feature to a module. The same approach will work for a component, but it is slightly different.

The file you want to copy is
/components/com_content/views/category/tmpl/default_articles.php
and you'd override it under the template's html directory...
/templates/<my-template>/html/com_content/category/default_articles.html

The title is displayed in possibly two places within this file. Search for $article->title and you will find them. The first occurrance is the normal occurrance (the latter is less frequently used - its for when an article is listed but not accessible based upon the user's credentials). So focus first on the first occurrance.

For me, this was on line 106. What I did was add my code to the <td> tag a couple of lines earlier. Here is that line of code after I changed it:

Code: Select all

<td class="list-title <?php echo ($article->featured)? 'featured-article':'';  ?>">
You could also wrap the anchor <a> tag in div that inserts the class for featured items.

Code: Select all

<div class="<?php echo ($article->featured)? 'featured-article':'';  ?>">
...whatever it takes and suits you.

If you want an alternative component layout (actually, a new menu item type), let me know. That is more involved, but do-able.

Report back with the results, if you don't mind.
Randy Carey -- as of 2023 I'm mostly retired in web development, but still engaged with a few Joomla projects through
Careytech Studios http://careytech.com custom development for tailored or value-added web solutions

elavd
Joomla! Apprentice
Joomla! Apprentice
Posts: 6
Joined: Thu Nov 04, 2010 6:54 pm

Re: Different CSS style for Featured articles in a list

Post by elavd » Fri Feb 08, 2013 5:04 pm

THANK YOU my friend!!!!!!! Your answer helped me and I fixed it!!!

In fact I've directly changed the code of the file:
/components/com_content/views/category/tmpl/default_articles.php

without copying to my Template's folder.

Do you think that it's safer to place my custom code to the Template's folder?

THANK YOU ONCE AGAIN! :)

anotheruser
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 172
Joined: Fri Oct 10, 2008 1:45 am

Re: Different CSS style for Featured articles in a list

Post by anotheruser » Fri Feb 08, 2013 6:27 pm

You really should use the override technique as rcarey has described so nicely. The reason for taking that approach is because it protects your changes from being overwritten by future updates.

Editing core files directly is not a good idea and should be avoided where possible.

User avatar
rcarey
Joomla! Explorer
Joomla! Explorer
Posts: 471
Joined: Sat Apr 25, 2009 9:20 pm
Location: Minnesota (USA)
Contact:

Re: Different CSS style for Featured articles in a list

Post by rcarey » Fri Feb 08, 2013 7:06 pm

Very glad I could help.

Yes, it is highly recommended that you place your changes in the override folder (under the template's /html folder). ...for the upgrade reasons "anotheruser" stated.
Randy Carey -- as of 2023 I'm mostly retired in web development, but still engaged with a few Joomla projects through
Careytech Studios http://careytech.com custom development for tailored or value-added web solutions

elavd
Joomla! Apprentice
Joomla! Apprentice
Posts: 6
Joined: Thu Nov 04, 2010 6:54 pm

Re: Different CSS style for Featured articles in a list

Post by elavd » Fri Feb 08, 2013 10:51 pm

Thank you both once again!!!
I owe you one! ;)

User avatar
uaintgotthisid
Joomla! Explorer
Joomla! Explorer
Posts: 367
Joined: Wed Sep 10, 2008 6:05 pm
Location: Essex, England, United Kingdom
Contact:

Re: Different CSS style for Featured articles in a list

Post by uaintgotthisid » Wed Apr 20, 2016 5:08 pm

rcarey wrote:I have a solution for you. This is a technique I use in my projects.

The key is to add a CSS class to the list item if the item is featured. That way you can use CSS to add the styling you want to titles of featured items - but of course, that styling will not be applied to non-featured items.

Assuming you want to apply this styling for the module that lists articles within a category, you will need to override that modules view file (or create an alternative layout for it). The file to override is
/modules/mod_articles_category/tmpl/default.php
You should place a copy of this file here
/templates/<my template>/html/mod_articles_category/default.php
Create the mod_articles_category directory if needed. Now this copied file should be overriding the original file.

Within this file, go to about line 86 (shortly after the "else" statement). replace the line

Code: Select all

<li>
with this

Code: Select all

<li <?php echo ($item->featured) ? 'class="featured-title"' : ''; ?> >
This does exactly what I stated for a goal - if the item is featured, we add a CSS class to the list item. You can name the class whatever you want, but I chose featured-title. Now you just add the styling as you want leveraging the presence of this selectively added CSS class. Keep in mind that you might need to create a CSS rule based upon multiple selectors, such as
li.featured-title a

The file mentioned handles grouping of items by their subcategories. If you are using that option, you will need to apply the change I stated on the other lines that add <li> to the HTML.

Let me add that creating an alternative layout is the approach to take in case you want some modules to behave this way, and others not to do so. All you need to do is copy the original default.php and give it a new name (like highlighted.php). This file can sit either directory mentioned above. Then apply the change to the new file. Once that is in place... If a module is to highlight featured items, select the alternative layout of "highlight." Modules left with the "default" layout will display using the default.php template.
This seemed useful for anyone, so I added a pull request for the Category Blog View.
Joomla lover, web designer, marketeer
https://www.squareballoon.co.uk
JOIN US at Joomla! User Group London
https://www.joomlalondon.co.uk


Locked

Return to “Administration Joomla! 2.5”