2

I have another question which was kindly answered by @Lodder here:

Echo Category form field type title in Module

In the link which is produced, I want to separate the link and title from the category, meaning I can add the link to a parent button and the title within that button - how can this be done using the following html:

<div class="row">
    <div class="col-md-<?php echo $buttonWidth;?>">
        <div class="row">
        <a href="<?php echo $categoryLinkOne; ?>">
            <button class="btn btn-default col-md-12" type="button">
                <div class="col-md-4">
                    <img class="img-responsive center-block" src="<?php echo $moduleLoc,$iconOne; ?>" />
                </div>
                <div class="col-md-8">
                    <?php echo $categoryTitleOne; ?></h3>
                </div>
            </button>
        </a>
        </div>

    <?php if($showIntroTextOne==1):?>
        <div class="row">
            <div class="col-md-12">
                <p><?php echo $introTextOne; ?></p>
            </div>
        </div>
    <?php endif; ?>
    </div>

<?php if($params->get('useTwo')==1){?>  
    <div class="col-md-<?php echo $buttonWidth;?>">
        <div class="row">
        <a href="<?php echo $categoryLinkTwo; ?>">
            <button class="btn btn-default col-md-12" type="button">
                <div class="col-md-4">
                    <img class="img-responsive center-block" src="<?php echo $moduleLoc,$iconTwo; ?>" />
                </div>
                <div class="col-md-8">
                    <?php echo $categoryTitleTwo; ?></h3>
                </div>
            </button>
        </a>
        </div>

    <?php if($showIntroTextTwo==1):?>
        <div class="row">
            <div class="col-md-12">
                <p><?php echo $introTextTwo; ?></p>
            </div>
        </div>
    <?php endif; ?>
    </div>
<?php } ?>
</div>
1
  • Could you provide a HTML markup example of what you're trying to achieve?
    – Lodder
    Sep 30, 2015 at 8:17

1 Answer 1

1

I'll use the function I wrote for this.

So, we're going to add another argument to the function to define the type of button/link we want to display and set a default value of normal

function displayCategoryLink($id, $type = 'normal')
{
    require_once JPATH_SITE . '/components/com_content/helpers/route.php';

    $db = JFactory::getDbo();

    $query = $db->getQuery(true);
    $query->select($db->quoteName('title'))
          ->from($db->quoteName('#__categories'))
          ->where($db->quoteName('id') . ' = '. (int)$id);
    $db->setQuery($query);

    try
    {
        $result = $db->loadResult();
    }
    catch (Exception $e)
    {
        $result = '';
    }

    $link = JRoute::_('index.php?option=com_content&view=category&layout=blog&id=' . $id); 
    
    if ($type == 'extended')
    {
        $html = '
        <a href="' . $link . '">
            <button class="btn btn-default col-md-12" type="button">
                <div class="col-md-4">
                    <img class="img-responsive center-block" src="' . $moduleLoc,$iconOne . '" />
                </div>
                <div class="col-md-8">
                    <h3>' .  $result . '</h3>
                </div>
            </button>
        </a>';
    }
    else
    {
        $html = '<a href="' . $link . '">' . $result . '</a>';
    }

    return $html;
}


displayCategoryLink($params->get('catOne', 0));
displayCategoryLink($params->get('catTwo', 0));
displayCategoryLink($params->get('catThree', 0), 'extended');
displayCategoryLink($params->get('catFour', 0), 'extended');

Please note that you're also using $moduleLoc,$iconOne for the image source, so you'll either need to pass these values through the function as an argument, or define them yourself inside the function.

Update:

Here if your updated code that should be in the default.php

<div class="row">
    <div class="col-md-<?php echo $buttonWidth;?>">
        <div class="row">
            <?php displayCategoryLink($params->get('catOne', 0), 'extended'); ?>
        </div>

        <?php if($showIntroTextOne == 1):?>
            <div class="row">
                <div class="col-md-12">
                    <p><?php echo $introTextOne; ?></p>
                </div>
            </div>
        <?php endif; ?>
    </div>

    <?php if($params->get('useTwo') == 1) : ?>  
        <div class="col-md-<?php echo $buttonWidth;?>">
            <div class="row">
                <?php displayCategoryLink($params->get('catTwo', 0), 'extended'); ?>
            </div>

            <?php if($showIntroTextTwo == 1):?>
                <div class="row">
                    <div class="col-md-12">
                        <p><?php echo $introTextTwo; ?></p>
                    </div>
                </div>
            <?php endif; ?>
        </div>
    <?php endif; ?>
</div>

Hope this helps

5
  • Thanks, but how can this work if I have multiple buttons with different categories? It works great for one button, but what about two, three or four buttons? I've updated the HTML example above Sep 30, 2015 at 9:30
  • @RustyJoomla - I've updated my answer implementing the function in the code you provided in your question
    – Lodder
    Sep 30, 2015 at 9:38
  • Thanks Lodder, this isn't outputting the variables in the frontend though unfortunately. It's only showing the '$showIntroText###' data Sep 30, 2015 at 10:45
  • @RustyJoomla - chat.stackexchange.com/rooms/29706/lodder-rusty
    – Lodder
    Sep 30, 2015 at 10:54
  • Can't comment in the chat room :( Sep 30, 2015 at 11:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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