1

I'd like to get articles of a certain attribute, how do I achieve it.
Below is a code I get to get an article of an id:

jimport('joomla.application.component.model');
JModel::addIncludePath(JPATH_SITE . '/components/com_content/models');
$Model = JModel::getInstance('Article', 'ContentModel');
$articleId = '1'; //We used here '1' article id
$article = $Model->getItem($articleId);
2

In the most generic way, you can write a query to load the articles in a list. Or, you can work with the article model.

You can check how it works in a content module or in content component:

mod_articles_latest

modules/mod_articles_latest/helper.php

    // Get an instance of the generic articles model
    $model = JModelLegacy::getInstance('Articles', 'ContentModel', array('ignore_request' => true));

    // Set application parameters in model
    $app       = JFactory::getApplication();
    $appParams = $app->getParams();
    $model->setState('params', $appParams);

    // Set the filters based on the module params
    $model->setState('list.start', 0);
    $model->setState('list.limit', (int) $params->get('count', 5));
    $model->setState('filter.published', 1);

    // Access filter
    $access     = !JComponentHelper::getParams('com_content')->get('show_noauth');
    $authorised = JAccess::getAuthorisedViewLevels(JFactory::getUser()->get('id'));
    $model->setState('filter.access', $access);

    // Category filter
    $model->setState('filter.category_id', $params->get('catid', array()));

    // User filter
    $userId = JFactory::getUser()->get('id');

    switch ($params->get('user_id'))
    {
        case 'by_me' :
            $model->setState('filter.author_id', (int) $userId);
            break;
        case 'not_me' :
            $model->setState('filter.author_id', $userId);
            $model->setState('filter.author_id.include', false);
            break;

        case '0' :
            break;

        default:
            $model->setState('filter.author_id', (int) $params->get('user_id'));
            break;
    }

    // Filter by language
    $model->setState('filter.language', $app->getLanguageFilter());

    //  Featured switch
    switch ($params->get('show_featured'))
    {
        case '1' :
            $model->setState('filter.featured', 'only');
            break;
        case '0' :
            $model->setState('filter.featured', 'hide');
            break;
        default :
            $model->setState('filter.featured', 'show');
            break;
    }

    // Set ordering
    $order_map = array(
        'm_dsc' => 'a.modified DESC, a.created',
        'mc_dsc' => 'CASE WHEN (a.modified = ' . $db->quote($db->getNullDate()) . ') THEN a.created ELSE a.modified END',
        'c_dsc' => 'a.created',
        'p_dsc' => 'a.publish_up',
        'random' => 'RAND()',
    );
    $ordering = JArrayHelper::getValue($order_map, $params->get('ordering'), 'a.publish_up');
    $dir      = 'DESC';

    $model->setState('list.ordering', $ordering);
    $model->setState('list.direction', $dir);

    $items = $model->getItems();

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.