1

Hi I'm learning the php and this has gone way over my head. I'm trying to make the blog item page links go to an external link if the A-link in the article is entered. I found some code in the com_content/article/default_link.php. I tried to just place the code in, before I tried to do an else command 'blog_style_default_title.php' but not work. Basically want to have the image and links to change to A-record if one is entered, otherwise go to article as normal.

Code below: original and code trying to apply:

<?php
/**
 * @package     Joomla.Site
 * @subpackage  Layout
 *
 * @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;

// Create a shortcut for params.
$params = $displayData->params;
$canEdit = $displayData->params->get('access-edit');
JHtml::addIncludePath(JPATH_COMPONENT.'/helpers/html');
?>
<?php if ($params->get('show_title') || $displayData->state == 0 || ($params->get('show_author') && !empty($displayData->author ))) : ?>
    <div class="page-header">

        <?php if ($params->get('show_title')) : ?>
            <h2 itemprop="name">
                <?php if ($params->get('link_titles') && $params->get('access-view')) : ?>
                    <a href="<?php echo JRoute::_(ContentHelperRoute::getArticleRoute($displayData->slug, $displayData->catid, $displayData->language)); ?>" itemprop="url">
                    <?php echo $this->escape($displayData->title); ?></a>
                <?php else : ?>
                    <?php echo $this->escape($displayData->title); ?>
                <?php endif; ?>
            </h2>
        <?php endif; ?>

        <?php if ($displayData->state == 0) : ?>
            <span class="label label-warning"><?php echo JText::_('JUNPUBLISHED'); ?></span>
        <?php endif; ?>
        <?php if (strtotime($displayData->publish_up) > strtotime(JFactory::getDate())) : ?>
            <span class="label label-warning"><?php echo JText::_('JNOTPUBLISHEDYET'); ?></span>
        <?php endif; ?>
        <?php if ((strtotime($displayData->publish_down) < strtotime(JFactory::getDate())) && $displayData->publish_down != JFactory::getDbo()->getNullDate()) : ?>
            <span class="label label-warning"><?php echo JText::_('JEXPIRED'); ?></span>
    <?php endif; ?>
    </div>
<?php endif; ?>




<?php
// ##################### Code from  default_link ########## //

// Create shortcut
$urls = json_decode($this->item->urls);

// Create shortcuts to some parameters.
$params = $this->item->params;
if ($urls && (!empty($urls->urla))) :
?>
<div class="content-links">
        <?php
            $urlarray = array(
            array($urls->urla, $urls->urlatext, $urls->targeta, 'a'),
            );
            foreach ($urlarray as $url) :
                $link = $url[0];
                $label = $url[1];
                $target = $url[2];
                $id = $url[3];

                if ( ! $link) :
                    continue;
                endif;

                // If no label is present, take the link
                $label = ($label) ? $label : $link;

                // If no target is present, use the default
                $target = $target ? $target : $params->get('target' . $id);
                ?>
                <?php
                    // Compute the correct link

                    switch ($target)
                    {
                        case 1:
                            // open in a new window
                            echo '<a href="' . htmlspecialchars($link) . '" target="_blank"  rel="nofollow">' .
                                htmlspecialchars($label) . '</a>';
                            break;

                        default:
                            // open in parent window
                            echo '<a href="' . htmlspecialchars($link) . '" rel="nofollow">' .
                                htmlspecialchars($label) . ' </a>';
                            break;
                    }
                ?>
        <?php endforeach; ?>
</div>
<?php endif; ?>
2
  • Welcome to JSE. Please could you format all your code so it's readable?
    – Lodder
    Jul 22, 2015 at 12:46
  • Thank you Lodder, I had it initially formatted and copied it in, and then it displayed as it did. so wasn't sure if that was all I could do, sorry Jul 22, 2015 at 12:55

1 Answer 1

1

Finally worked it out.

Ok so in the layout, you cannot simply use $this->item like you would in a view. The object to be used is $displayData.

So to get the URL param you want, open the following file:

ROOT/layouts/joomla/content/blog_style_default_title.php

and add the flowing to the top:

$urls = json_decode($displayData->urls);

$urlarray = array(
    array($urls->urla)
);

You will then see the following code which is used for the article link:

<a href="<?php echo JRoute::_(ContentHelperRoute::getArticleRoute($displayData->slug, $displayData->catid, $displayData->language)); ?>" itemprop="url">

which needs to be replaced with this:

<?php if (!empty($urlarray[0][0])) : ?>
    <a href="<?php echo $urlarray[0][0]; ?>" target="_blank">
<?php else : ?>
    <a href="<?php echo JRoute::_(ContentHelperRoute::getArticleRoute($displayData->slug, $displayData->catid, $displayData->language)); ?>" itemprop="url">
<?php endif; ?>

Add you can see, $urlarray[0][0] is the variable you now need to use for the Link A value.

Hope this helps

3
  • This has worked perfectly - fantastic. The final element is the intro_image.php file. to make the image also go to the A-link... I tried to apply but the code had an error.. Jul 27, 2015 at 10:34
  • @MartinEarl - Replace the code in the intro_image.php file with this: pastebin.com/G9YkPeZr
    – Lodder
    Jul 27, 2015 at 10:45
  • Thank you - worked a charm for intro_image too. Such a great help, perfect Jul 27, 2015 at 10:58

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.