Mohammad Arshi
Mohammad Arshi

Reputation: 386

how to get article from the specific category joomla

In joomla 2.5 is there any built in function to fetch all article from the specific category. I don't want to use custom query to fetch article from the database.

Upvotes: 0

Views: 6694

Answers (6)

Lightwaxx
Lightwaxx

Reputation: 765

In Joomla 5, this is fairly easy and straight forward. See below

use Joomla\CMS\Factory;

$app = Factory::getApplication();

$model = $app->bootComponent('com_content')->getMVCFactory()->createModel('Articles', 'Site', ['ignore_request' => true]);    
$model->setState('params', Factory::getApplication()->getParams()); // Set parameters. Won't work without this filter
$model->setState('filter.category_id', $params->get('category_id')); // Category id probably set up in your module as a parameter
$model->setState('filter.published', 1); // Only Published articles
$model->setState('filter.access', 1); // Set access to public
$model->setState('filter.featured', 'only'); // Only featured articles

// List Params
$model->setState('list.start', 0);
$model->setState('list.limit', 4);

$articles = $model->getItems();

Please refer to components\com_content\src\Model\CategoryModel.php from line 236 to get all the filters you can set.

Upvotes: 0

user7194542
user7194542

Reputation:

I create small function to get category and return all articles with custom fields

function getArticlesByCategory($categoryId, $limit = "")
{
    if (is_int($limit) && !empty($limit)) {
        $limit = "LIMIT $limit";
    }

    $articles = JFactory::getDBO()->setQuery('
    SELECT
        C.*,
        CONCAT(
            "{",
            GROUP_CONCAT(CONCAT(\'"\',F.name,\'"\', ":",\'"\',V.value,\'"\')),
            "}"
        ) AS \'additional_fields\'
    FROM
        #__content AS C
    LEFT JOIN #__fields_values AS V
    ON
        V.item_id = C.id
    LEFT JOIN #__fields AS F
    ON
        F.id = V.field_id
    WHERE
        C.catid = \'' . $categoryId . '\' AND F.context = "com_content.article"
    GROUP BY
        C.id ' . $limit
    )->loadObjectList();

    if($articles){
        foreach ($articles as $article) {
            $article->additional_fields = json_decode($article->additional_fields);
        }
    }

    return $articles;
}

Usage

$categoryId = 9;
$limit = 4;
getArticlesByCategory(9,2);

Tested on Joomla! 3.8.11

I don't like solutions like this one, but Joomla! made me do it.

Upvotes: 2

Lodder
Lodder

Reputation: 19743

I'm not sure how're you're setting up your slider however to answer your actual question:

$db = JFactory::getDbo();     
$query = $db->getQuery(true);     
$query->select('*')
      ->from($db->quoteName('#__content'))
      ->where($db->quoteName('catid') . ' = 2');    
$db->setQuery($query);     
$rows = $db->loadObjectList();

foreach ( $rows as $row ) {
    echo $row->introtext;
}

There is a built in function to get articles using the route.php I believe which is what the likes of Accordions use and so on, however for the most simple method, use the above.

Note: Don't forget to change the catid value from 2 to whatever suits your needs.

Hope this helps

Upvotes: 3

Mads
Mads

Reputation: 11

If you are creating a module, you may just create a module that has a type Article Category. Then, on the right side, click on the Filtering Options and there, select the category you want to show.

Upvotes: 0

Laoneo
Laoneo

Reputation: 1566

I would use the articles model

JLoader::import('joomla.application.component.model');
JModelLegacy::addIncludePath(JPATH_SITE.'/components/com_content/models', 'ContentModel');
$model = JModelLegacy::getInstance('Articles', 'ContentModel');
$model->getState();
$model->setState('list.limit', 10);

$articles = $model->getItems();

Upvotes: 4

Riccardo Zorn
Riccardo Zorn

Reputation: 5615

Check this out: /modules/mod_articles_category for a full-fledged (and fairly slow) implementation. You might want to make it simpler:

select introtext, params from #__content where catid=%s AND state=1 AND ...

(you might want to add some checks on publish_up fields etc, but if you're happy with managing published/unpublished and don't use publish_up / down you don't need to do this).

Make sure you implement the module correctly to leverage Joomla cache, even if this query is fast it's best to avoid repeating it adlib. Read this for more details on module's cache

Upvotes: 0

Related Questions