Display Categories in a Dropdown?

General questions relating to Joomla! 2.5. Note: All 1.6 and 1.7 releases have reached end of life and should be updated to 2.5. There are other boards for more specific help on Joomla! features and extensions.

Moderator: General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting.
Forum Post Assistant - If you are serious about wanting help, you should use this tool to help you post.
Locked
User avatar
numinousmedia
Joomla! Ace
Joomla! Ace
Posts: 1567
Joined: Fri Dec 16, 2011 6:13 pm
Location: Barberton, OH
Contact:

Display Categories in a Dropdown?

Post by numinousmedia » Thu Sep 19, 2013 5:17 pm

Is it possible to display categories in an automatically generated dropdown list? Ideally, I'd have four different parent categories, each with 10 - 15 children categories (in 4 different dropdowns). When a category is clicked in a drop down, it would display the contents in that category in a blog layout.

I'm open to any ideas or suggestions.
Ryan
Frontend Developer and Joomla Professional
Ethode Website Development: http://www.ethode.com
Personal Site: http://www.numinousmedia.com

User avatar
FidelGonzales
Joomla! Guru
Joomla! Guru
Posts: 584
Joined: Thu Nov 03, 2005 12:10 am
Location: Hesperia, California, USA
Contact:

Re: Display Categories in a Dropdown?

Post by FidelGonzales » Thu Sep 19, 2013 7:39 pm

I would assume there are multiple modules that can accomplish this via the Joomla Extensions Directory. Since Joomla 1.0, I have not dealt with Joomla Content Component and would recommend considering the K2 component, as there are multiple standard and advanced modules and plugins that enable you to accomplish this.
http://www.MediaArmory.com - WEB | PHOTO | WRITE | MARKETING | DESIGN
http://www.DirtArmory.com - Off Road Sports Lifestyle

User avatar
numinousmedia
Joomla! Ace
Joomla! Ace
Posts: 1567
Joined: Fri Dec 16, 2011 6:13 pm
Location: Barberton, OH
Contact:

Re: Display Categories in a Dropdown?

Post by numinousmedia » Thu Sep 19, 2013 7:54 pm

Yeah... welp, there really weren't any good extensions that would do this, and only this. I'm definitely not going to tangle with a big component like K2 just to get categories to display in a dropdown field. Fortunately, I did figure out how to tweak one of native Joomla modules to make this happen just about perfectly.

This requires making a small change to one of the core Joomla files... if you aren't comfortable doing this, I would avoid this solution. Otherwise...

How to Display Joomla Categories in a Dropdown Field:

The module of choice is the "Articles Categories" module. Natively, this module displays a list of categories in your website. I was able to adapt this module to display that list as options in a dropdown field instead.

You will need to edit this file: /modules/mod_articles_categories/tmpl/default_items.php

The original code looks like this:

Code: Select all

<?php
/**
 * @package		Joomla.Site
 * @subpackage	mod_articles_categories
 * @copyright	Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
 * @license		GNU General Public License version 2 or later; see LICENSE.txt
 */

// no direct access
defined('_JEXEC') or die;
foreach ($list as $item) :

?>
	<li <?php if ($_SERVER['PHP_SELF'] == JRoute::_(ContentHelperRoute::getCategoryRoute($item->id))) echo ' class="active"';?>> <?php $levelup=$item->level-$startLevel -1; ?>
  <h<?php echo $params->get('item_heading')+ $levelup; ?>>
		<a href="<?php echo JRoute::_(ContentHelperRoute::getCategoryRoute($item->id)); ?>">
		<?php echo $item->title;?></a>
   </h<?php echo $params->get('item_heading')+ $levelup; ?>>

		<?php
		if($params->get('show_description', 0))
		{
			echo JHtml::_('content.prepare', $item->description, $item->getParams(), 'mod_articles_categories.content');
		}
		if($params->get('show_children', 0) && (($params->get('maxlevel', 0) == 0) || ($params->get('maxlevel') >= ($item->level - $startLevel))) && count($item->getChildren()))
		{

			echo '<ul>';
			$temp = $list;
			$list = $item->getChildren();
			require JModuleHelper::getLayoutPath('mod_articles_categories', $params->get('layout', 'default').'_items');
			$list = $temp;
			echo '</ul>';
		}
		?>
 </li>
<?php endforeach; ?>
My tweaked code looks like this:

Code: Select all

<select>
<?php
/**
 * @package		Joomla.Site
 * @subpackage	mod_articles_categories
 * @copyright	Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
 * @license		GNU General Public License version 2 or later; see LICENSE.txt
 */

// no direct access
defined('_JEXEC') or die;
foreach ($list as $item) :

?>
	<option onclick="window.location = '<?php echo JRoute::_(ContentHelperRoute::getCategoryRoute($item->id)); ?>'" <?php if ($_SERVER['PHP_SELF'] == JRoute::_(ContentHelperRoute::getCategoryRoute($item->id))) echo ' class="active"';?>> <?php $levelup=$item->level-$startLevel -1; ?>
  <h<?php echo $params->get('item_heading')+ $levelup; ?>>
		<?php echo $item->title;?>
</h<?php echo $params->get('item_heading')+ $levelup; ?>>

		<?php
		if($params->get('show_description', 0))
		{
			echo JHtml::_('content.prepare', $item->description, $item->getParams(), 'mod_articles_categories.content');
		}
		if($params->get('show_children', 0) && (($params->get('maxlevel', 0) == 0) || ($params->get('maxlevel') >= ($item->level - $startLevel))) && count($item->getChildren()))
		{

			echo '<ul>';
			$temp = $list;
			$list = $item->getChildren();
			require JModuleHelper::getLayoutPath('mod_articles_categories', $params->get('layout', 'default').'_items');
			$list = $temp;
			echo '</ul>';
		}
		?>
 </option>
<?php endforeach; ?>
</select>

Basically, the list elements are replaced with field elements. There was also the need to change how the link is created to conform to a list style "on-click" route instead of the normal "<a href".

Along with making this tweak to the code, I changed the "Maximum Level Depth" to 1 in the Module Parameters.

Hopefully this helps someone tackle this issue without investing tons of time reading up on it :-)
Ryan
Frontend Developer and Joomla Professional
Ethode Website Development: http://www.ethode.com
Personal Site: http://www.numinousmedia.com

User avatar
FidelGonzales
Joomla! Guru
Joomla! Guru
Posts: 584
Joined: Thu Nov 03, 2005 12:10 am
Location: Hesperia, California, USA
Contact:

Re: Display Categories in a Dropdown?

Post by FidelGonzales » Thu Sep 19, 2013 8:03 pm

Excellent. Thanks for adding your solution.
http://www.MediaArmory.com - WEB | PHOTO | WRITE | MARKETING | DESIGN
http://www.DirtArmory.com - Off Road Sports Lifestyle

FishEagle
Joomla! Intern
Joomla! Intern
Posts: 88
Joined: Wed Nov 30, 2016 5:02 pm
Location: South Africa

Re: Display Categories in a Dropdown?

Post by FishEagle » Fri Nov 03, 2017 6:33 am

numinousmedia wrote:You will need to edit this file: /modules/mod_articles_categories/tmpl/default_items.php
Thanks for the solution!

BUT, is it possible to make these changes without editing the Joomla core file? Maybe an override? Or, creating another file applicable to the specific "Articles Categories"-module only?
I enjoy building websites on an ad-hoc charitable basis.

FishEagle
Joomla! Intern
Joomla! Intern
Posts: 88
Joined: Wed Nov 30, 2016 5:02 pm
Location: South Africa

Re: Display Categories in a Dropdown?

Post by FishEagle » Fri Nov 03, 2017 1:33 pm

Solution: Template override... (answering my own question, if someone ever have similar question)

Within Joomla; Extensions -> Templates -> Templates -> 'select your template' -> Create Overrides -> 'select e.g. applicable module'. Make changes to this file, thus not editing any Joomla core file(s).
I enjoy building websites on an ad-hoc charitable basis.


Locked

Return to “General Questions/New to Joomla! 2.5”