RhymeGuy
RhymeGuy

Reputation: 2122

Code to display articles from category ID?

What would be the code to display articles from category (specified by ID)?

In Wordpress this is fairly easy by doing:

<?php
   query_posts('cat=1');
   while (have_posts()) : the_post();
   the_title();
   endwhile;
?>

I'm looking for similar code for Joomla.

Upvotes: 3

Views: 11361

Answers (5)

Lightwaxx
Lightwaxx

Reputation: 765

This is quite easy in Joomla. You can find the code in /components/com_content/models/category.php under getItems()

Below is an example

$model = JModelLegacy::getInstance('Articles', 'ContentModel', array('ignore_request' => true));
$model->setState('params', JFactory::getApplication()->getParams());
$model->setState('filter.category_id', $category_id);

$articles = $model->getItems();

Below is the updated code for Joomla 5

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

Alex
Alex

Reputation: 2775

Try this:

$articles = JFactory::getDBO()->setQuery("SELECT * FROM #__content WHERE catid = '21'")->loadObjectList();

Of course replace '21' with id of your category.

Upvotes: 2

Ice
Ice

Reputation: 71

You can use next code template:

$categoryId = $[category id];

require_once("components/com_content/models/category.php");
$category = new ContentModelCategory();
$category->hit($categoryId);
$articles = $category->getItems();
print_r($articles);

Upvotes: 6

brunofitas
brunofitas

Reputation: 3083

$db = JFactory::getDbo();
$id = 50;//example

$query = $db->getQuery(true);
$query->select('*');
$query->from('#__content');
$query->where('catid="'.$id.'"');

$db->setQuery((string)$query);
$res = $db->loadObjectList();

foreach($res as $r){
    echo '<h3>'.$r->title.'</h3>';
    echo $r->introtext;
}

Upvotes: 12

Jobin
Jobin

Reputation: 8282

There is no direct code for getting this in joomla like word press.

If you want to check the code you can check the code for achieving this by following path.

components/com_content/view/category/view.html.php

and 
components/com_content/view/category/tmpl/blog.php

According to my Guess your requirement is to display the articles from same category.

then in joomla you dont need to edit in any code. for achieving this you can simply create a menu. and menu layout type should be category blog and Choose your category from the right side category options.

This will get the complete article from that category.

If you want to manage it in your style you can add style based on blog.php file .

Hope this will help you..

Upvotes: 2

Related Questions