这段代码来源于 Joomla 自带文章相关模块,如需要更多定义请 文章相关模块 以及 前台文章列表模块。

<?php
function getArticleList($option) {
    $defaultOption = array(
        'list.start' => 0,
        'list.limit' => 10,
        'filter.published' => 1,
        'filter.featured' => 0,
        'filter.category_id' => array(),
        'filter.language' => null,
        'list.ordering' => 'a.hits',
        'list.direction' => 'a.DESC',
        );
    $option = array_merge($defaultOption,$option);
    require_once JPATH_SITE.'/components/com_content/helpers/route.php';
    JModelLegacy::addIncludePath(JPATH_SITE.'/components/com_content/models','ContentModel');
    $model = JModelLegacy::getInstance('Articles','ContentModel',array('ignore_request' => true));
    $app = JFactory::getApplication();
    $appParams = $app->getParams();
    $model->setState('params',$appParams);
    $model->setState('list.start',$option['list.start']);
    $model->setState('list.limit',$option['list.limit']);
    $model->setState('filter.published',$option['filter.published']);
    $model->setState('filter.featured',$option['filter.featured'] == 1?'show':'hide');
    $access = !JComponentHelper::getParams('com_content')->get('show_noauth');
    $authorised = JAccess::getAuthorisedViewLevels(JFactory::getUser()->get('id'));
    $model->setState('filter.access',$access);
    $model->setState('filter.category_id',$option['filter.category_id']);
    $language = empty($option['filter.language'])?$app->getLanguageFilter():$option['filter.language'];
    $model->setState('filter.language',$language);
    $model->setState('list.ordering',$option['list.ordering']);
    $model->setState('list.direction',$option['list.direction']);
    $data = $model->getItems();
    foreach($data as &$item) {
        $item->slug = $item->id.':'.$item->alias;
        $item->catslug = $item->catid.':'.$item->category_alias;
        if($access || in_array($item->access,$authorised)) {
            $item->link = JRoute::_(ContentHelperRoute::getArticleRoute($item->slug,$item->catid,$item->language));
        } else {
            $item->link = JRoute::_('index.php?option=com_users&view=login');
        }
        $item->image = json_decode($item->images)->image_intro;
    }
}
?>