1

I have created an override of "mod_articles_category/default.php" to get all the outputs from the article category module in table rows. This worked fine so far and it is displaying the three selected columns "Title", "Category" and "Date".

I now need to add another column "Parent Category". Where/how can I get the parent category of the listed articles?

This article suggested the following code for quite a similar problem (Joomla 3.3):

<?php
$db = JFactory::getDbo();
$id = $item->core_catid;
$db->setQuery("SELECT cat.parent_id FROM #__categories cat WHERE cat.id='$id'");
$parent_category_id = $db->loadResult();
$db->setQuery("SELECT cat.title FROM #__categories cat WHERE cat.id='$parent_category_id'");
$parent_category_title = $db->loadResult();
if($parent_category_title != 'ROOT'){
 echo '<p>Parent Category: '.$parent_category_title.'</p>';
}
echo '<p>Parent Category: <a href="index.php?option=com_content&view=category&layout=blog&id='.$parent_category_id.'">'.$parent_category_title.'</a></p>';
?>

But this code does not work for me (Joomla 3.4.8), there is nothing returned for $parent_category_title. What is wrong here?

Also I am not sure how clean my solution would be in the end with my override looking like this:

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

defined('_JEXEC') or die;

?>

<table class="category-module<?php echo $moduleclass_sfx; ?>">
    <?php if ($grouped) : ?>
        <?php foreach ($list as $group_name => $group) : ?>
        <tr><td>
            <div class="mod-articles-category-group"><?php echo $group_name;?></div>
            <table>
                <?php foreach ($group as $item) : ?>
                    <tr>
                        <?php if ($params->get('link_titles') == 1) : ?>
                            <td><a class="mod-articles-category-title <?php echo $item->active; ?>" href="<?php echo $item->link; ?>">
                                <?php echo $item->title; ?>
                            </a></td>
                        <?php else : ?>
                            <td><?php echo $item->title; ?></td>
                        <?php endif; ?>


                        <?php if ($item->displayHits) : ?>
                            <td><span class="mod-articles-category-hits">
                                (<?php echo $item->displayHits; ?>)
                            </span></td>
                        <?php endif; ?>

                        <?php if ($params->get('show_author')) : ?>
                            <td><span class="mod-articles-category-writtenby">
                                <?php echo $item->displayAuthorName; ?>
                            </span></td>
                        <?php endif;?>

                        <?php if ($item->displayCategoryTitle) : ?>
                            <td><span class="mod-articles-category-category">
                                <?php echo strip_tags($item->displayCategoryTitle); ?>
                                </span>
                            </td>
                            <td>
                                <?php
                                    $db = JFactory::getDbo();
                                    $id = $item->core_catid;
                                    $db->setQuery("SELECT cat.parent_id FROM #__categories cat WHERE cat.id='$id'");
                                    $parent_category_id = $db->loadResult();
                                    $db->setQuery("SELECT cat.title FROM #__categories cat WHERE cat.id='$parent_category_id'");
                                    $parent_category_title = $db->loadResult();
                                    if($parent_category_title != 'ROOT'){
                                     echo '<span>'.$parent_category_title.'</span>';
                                    }
                                    ?>

                            </td>
                        <?php endif; ?>

                        <?php if ($item->displayDate) : ?>
                            <td><span class="mod-articles-category-date"><?php echo $item->displayDate; ?></span>
                        <?php endif; ?>

                        <?php if ($params->get('show_introtext')) : ?>
                            <td><p class="mod-articles-category-introtext">
                                <?php echo $item->displayIntrotext; ?>
                            </p></td>
                        <?php endif; ?>

                        <?php if ($params->get('show_readmore')) : ?>
                            <td><p class="mod-articles-category-readmore">
                                <a class="mod-articles-category-title <?php echo $item->active; ?>" href="<?php echo $item->link; ?>">
                                    <?php if ($item->params->get('access-view') == false) : ?>
                                        <?php echo JText::_('MOD_ARTICLES_CATEGORY_REGISTER_TO_READ_MORE'); ?>
                                    <?php elseif ($readmore = $item->alternative_readmore) : ?>
                                        <?php echo $readmore; ?>
                                        <?php echo JHtml::_('string.truncate', $item->title, $params->get('readmore_limit')); ?>
                                            <?php if ($params->get('show_readmore_title', 0) != 0) : ?>
                                                <?php echo JHtml::_('string.truncate', ($this->item->title), $params->get('readmore_limit')); ?>
                                            <?php endif; ?>
                                    <?php elseif ($params->get('show_readmore_title', 0) == 0) : ?>
                                        <?php echo JText::sprintf('MOD_ARTICLES_CATEGORY_READ_MORE_TITLE'); ?>
                                    <?php else : ?>
                                        <?php echo JText::_('MOD_ARTICLES_CATEGORY_READ_MORE'); ?>
                                        <?php echo JHtml::_('string.truncate', ($item->title), $params->get('readmore_limit')); ?>
                                    <?php endif; ?>
                                </a>
                            </p></td>
                        <?php endif; ?>
                    </tr>
                <?php endforeach; ?>
            </table>
        </tr>
        <?php endforeach; ?>
    <?php else : ?>
        <?php foreach ($list as $item) : ?>
            <tr>
                <?php if ($params->get('link_titles') == 1) : ?>
                    <td><a class="mod-articles-category-title <?php echo $item->active; ?>" href="<?php echo $item->link; ?>">
                        <?php echo $item->title; ?>
                    </a></td>
                <?php else : ?>
                    <td><?php echo $item->title; ?>
                <?php endif; ?>

                <?php if ($item->displayHits) : ?>
                    <td><span class="mod-articles-category-hits">
                        (<?php echo $item->displayHits; ?>)
                    </span></td>
                <?php endif; ?>

                <?php if ($params->get('show_author')) : ?>
                    <td><span class="mod-articles-category-writtenby">
                        <?php echo $item->displayAuthorName; ?>
                    </span></td>
                <?php endif;?>

                <?php if ($item->displayCategoryTitle) : ?>
                    <td><span class="mod-articles-category-category">
                        <?php echo $item->displayCategoryTitle; ?>
                    </span></td>
                <?php endif; ?>

                <?php if ($item->displayDate) : ?>
                    <td><span class="mod-articles-category-date">
                        <?php echo $item->displayDate; ?>
                    </span></td>
                <?php endif; ?>

                <?php if ($params->get('show_introtext')) : ?>
                    <td><p class="mod-articles-category-introtext">
                        <?php echo $item->displayIntrotext; ?>
                    </p></td>
                <?php endif; ?>

                <?php if ($params->get('show_readmore')) : ?>
                    <td><p class="mod-articles-category-readmore">
                        <a class="mod-articles-category-title <?php echo $item->active; ?>" href="<?php echo $item->link; ?>">
                            <?php if ($item->params->get('access-view') == false) : ?>
                                <?php echo JText::_('MOD_ARTICLES_CATEGORY_REGISTER_TO_READ_MORE'); ?>
                            <?php elseif ($readmore = $item->alternative_readmore) : ?>
                                <?php echo $readmore; ?>
                                <?php echo JHtml::_('string.truncate', $item->title, $params->get('readmore_limit')); ?>
                            <?php elseif ($params->get('show_readmore_title', 0) == 0) : ?>
                                <?php echo JText::sprintf('MOD_ARTICLES_CATEGORY_READ_MORE_TITLE'); ?>
                            <?php else : ?>
                                <?php echo JText::_('MOD_ARTICLES_CATEGORY_READ_MORE'); ?>
                                <?php echo JHtml::_('string.truncate', $item->title, $params->get('readmore_limit')); ?>
                            <?php endif; ?>
                        </a>
                    </p></td>
                <?php endif; ?>
            </tr>
        <?php endforeach; ?>
    <?php endif; ?>
</table>

Thanks in advance!

0

Ok, I was finally able to figure it out. I don't know why the variables from the other article like "cat.id" and "cat.title" and the syntax are so different, but they did not really work in my environment. Here is the solution that worked for me on Joomla 3.4.8:

    $db = JFactory::getDbo();
    $title = strip_tags($item->displayCategoryTitle);
    $language = $item->language;

    $query = $db->getQuery(true);

    $query->select($db->quoteName('parent_id'));
    $query->from($db->quoteName('#__categories'));
    $query->where($db->quoteName('title')."=".$db->quote($title).' AND '.$db->quoteName('language')."=".$db->quote($language));

    $db->setQuery($query);

    $parentId = $db->loadResult();

    $query = $db->getQuery(true);

    $query->select($db->quoteName('title'));
    $query->from($db->quoteName('#__categories'));
    $query->where($db->quoteName('id')."=".$db->quote($parentId));

    $db->setQuery($query);

    $result = $db->loadResult();

    echo $result;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.